-1

One activity in my project is crashing. LogCat says "Null EXception " . Simple Adapter giving null exception. I have posted the feed_activity.java and feed_activity.XML file .

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FeedActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Your Feed");
    setContentView(R.layout.activity_feed);
    final   ListView listView =  findViewById(R.id.listview);
    final  List<Map<String,String>> wordData= new ArrayList<>();

               for (int i=1;i<=5;i++){
                  Map<String,String> wordInfo = new HashMap<>();
                  wordInfo.put("content","word 
                  content"+Integer.toString(i));
                  wordInfo.put("username","user"+Integer.toString(i));
                  wordData.add(wordInfo);

               }
    SimpleAdapter  simpleAdapter = new SimpleAdapter(this, 
    wordData,android.R.layout.simple_list_item_2, new String[] 
   {"content","username"}, new int[] 
   {android.R.id.text1,android.R.id.text2});
    listView.setAdapter(simpleAdapter);

           }
        }

[LogCat for feedactivity][2] XML Activity_feed :- ListView is used inside a constraint layout .

<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".FeedActivity">

<ListView
    android:id="@+id/listView"
    android:layout_width="395dp"
    android:layout_height="590dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

-1

You can try the following things -

  1. Try removing final keyword from list declaration.

  2. Try replacing

final ListView = findViewById(R.id.listview);

with

final ListView = (ListView) findViewById(R.id.listview);

  1. Check the id of listview one more time.