I'm creating a basic app on Android Studio with SQLite. I want to show some data in a listview on an activity that's not the activity_main, but this listview always returns null and then the compiler crashes due to a NullPointerException.
This is an app with a drawer layout menu. I will post only the xml code of the activity where I want to show data (maf.xml), and the MainActivity.java (without package and imports). The objects BD and Lobito are created in other java files and work correctly because I tried it in another project.
Can anyone help me? Thanks.
--
maf.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listViewItems"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
ListView listViewItems;
ArrayAdapter<String> adapter;
ArrayList<String> arrayList;
BD bd = new BD(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listViewItems = findViewById(R.id.listViewItems);
listAll();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.bandos, R.id.maf,
R.id.nav_tools, R.id.nav_share, R.id.maf)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
public void listAll() {
List<Lobito> lobitos = bd.listaTodosLobitos();
arrayList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, arrayList);
listViewItems.setAdapter(adapter);
for (Lobito l : lobitos) {
arrayList.add(l.getId() + "-" + l.getNome());
adapter.notifyDataSetChanged();
}
}
}
You are finding listViewItems from the activity_main.xml file, while you have declared it in maf.xml. That's why you are getting error.
So just replace this line
setContentView(R.layout.activity_main);
with
setContentView(R.layout.maf);
I moved the code referent to maf.xml to a new file MAF.java, and changed to setContentView(R.layout.maf). However, looks like the code in MAF.java don't run. There's something wrong?
MAF.java
public class MAF extends AppCompatActivity {
ListView listViewItems;
ArrayAdapter<String> adapter;
ArrayList<String> arrayList;
BD bd = new BD(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maf);
listViewItems = findViewById(R.id.listViewItems);
listAll();
}
public void listAll() {
List<Lobito> lobitos = bd.listaTodosLobitos();
arrayList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(MAF.this,
android.R.layout.simple_list_item_1, arrayList);
listViewItems.setAdapter(adapter);
for (Lobito l : lobitos) {
//Log.d("Lista", "\nID: " + l.getId() + " Nome: " + l.getNome());
arrayList.add(l.getId() + "-" + l.getNome());
adapter.notifyDataSetChanged();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.