I am new to android and don't have that much knowledge about the database.
I created class Named " topics
" in parse server and manually added " topicName
" column.
Now I want to retrive this topicName value/Strings in a Listview but I'm getting an error
Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
Any help would be appreciated.
Code:
public class topics_class1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
final ArrayList<String> topics1 = new ArrayList<>();
final ListView listView = findViewById(R.id.listView_topics_class1);
final ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, topics1);
ParseQuery<ParseObject> query = ParseQuery.getQuery("topics");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null){
for (int i=0; i< objects.size(); i++) {
topics1.add(objects.get(i).getString("topicName"));
}
listView.setAdapter(arrayAdapter);
}
else {
e.printStackTrace();
}
}
});
}
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@+id/listView_topics_class1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="89dp" />
Error itself explain
Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
you did not setContentView(layout)
. that's why u getting error try setting setContentView(R.layout.layoutname);
and get ListView accordingly
final ListView listView = (ListView)findViewById(R.id.listView_topics_class1);
Let me know if u have any other question
public class MainActivity extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, planets);
ParseQuery<ParseObject> query = ParseQuery.getQuery("topics");
//query.selectKeys(Arrays.asList("topicName"));
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null){
// Toast.makeText(topics_class1.this, "success", Toast.LENGTH_LONG).show();
for (int i=0; i< objects.size(); i++) {
topics1.add(objects.get(i).getString("topicName"));
}
listView.setAdapter(arrayAdapter);
}
//}
else {
e.printStackTrace();
}
}
});
}
}
Follow this way and implement it
XML Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layoutwidth="matchparent"
android:layoutheight="matchparent"
android:paddingBottom="@dimen/activityverticalmargin"
android:paddingLeft="@dimen/activityhorizontalmargin"
android:paddingRight="@dimen/activityhorizontalmargin"
android:paddingTop="@dimen/activityverticalmargin"
tools:context=".MainActivity" >
<ListView android:layoutwidth="fillparent"
android:layoutheight="fillparent"
android:id="@+id/listView">
</ListView>
</LinearLayout>
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.