Good, in my code I have an adapter that contains elements of my database, which I want to insert in textviews of another activity (with ids), the problem is that NullPointException throws me, the original idea was to directly pass the data from the sqlite to the textview but I did not find the way to access the values of the database, so I passed it to an array and it does not work, please if anyone knows how to solve this, I attach code. I also have the doubt about which layout I should display in setContentView (R.layout.activity_list); (Since text view is created in xml_activity) The error arises in Select with the listview.getChild
ListActivity (pass data to textviews)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView=(ListView) findViewById(R.id.ltvListaEventos);
BDSQLite bd= new BDSQLite(getApplicationContext(), "Horario", null,1);
db= bd.getReadableDatabase();
String sql="select * from Horario";
Cursor c;
String ora;
try {
c=db.rawQuery(sql,null);
arrayAdapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
if(c==null||c.getCount()==0) {
Toast.makeText(getBaseContext(), "No hay eventos disponibles", Toast.LENGTH_LONG).show();
}
if(c.moveToFirst()){
do {
ora=c.getString(1);
arrayAdapter.add(ora);
} while(c.moveToNext());
listView.setAdapter(arrayAdapter);
}
}catch (Exception ex) {
Toast.makeText(getApplication(), "Error: "+ex.getMessage(), Toast.LENGTH_SHORT).show();
this.finish();
}
// int count = listView.getAdapter().getCount();
for(int i = 0; i <= 5; i = i + 1)
{
String Select= (String) listView.getChildAt(i).toString();
if (i==0){
TextView TEXTO11=(TextView) findViewById(R.id.t11);
TEXTO11.setText(Select);
}
}
}
error:
ava.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.marcela.sqlitetet/com.example.marcela.sqlitetet.ListActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.marcela.sqlitetet.ListActivity.onCreate(ListActivity.java:70)
EDIT:
public class ListActivity extends AppCompatActivity {
//al mantener la wea apretada
private SQLiteDatabase db;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
setContentView(R.layout.activity_horario);
listView=(ListView) findViewById(R.id.ltvListaEventos);
BDSQLite bd= new BDSQLite(getApplicationContext(), "Horario", null,1);
db= bd.getReadableDatabase();
String sql="select * from Horario";
Cursor c;
String ora;
try {
c=db.rawQuery(sql,null);
arrayAdapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
if(c==null||c.getCount()==0) {
Toast.makeText(getBaseContext(), "No hay eventos disponibles", Toast.LENGTH_LONG).show();
}
if(c.moveToFirst()){
do {
ora=c.getString(1);
arrayAdapter.add(ora);
} while(c.moveToNext());
listView.setAdapter(arrayAdapter);
}
}catch (Exception ex) {
Toast.makeText(getApplication(), "Error: "+ex.getMessage(), Toast.LENGTH_SHORT).show();
this.finish();
}
int count = listView.getAdapter().getCount();
for(int i = 0; i <= count; i = i ++)
{
if (i==0){
TextView TEXTO11=(TextView) findViewById(R.id.t11);
TEXTO11.setText(listView.get(i));
}
}
}
}
Activity schedule (has the textview, attached fragment of the code).
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:id="@+id/fila1">
<TextView
android:layout_width="44dp"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text="@string/bloque_12"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="10sp"
/>
<TextView
android:layout_width="7dp"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/t11"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="2"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/t12"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="2"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/t13"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="2"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text=""
android:id="@+id/t14"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="2"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text=""
android:id="@+id/t15"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
Activity timetable:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horario);
Button boton = (Button) findViewById(R.id.boton1);
boton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(HORARIOActivity.this, Editar.class);
startActivity(i);
}
});
}
}