Reputation: 467
I've seen other people who have had the same issue, but they were missing setContentView(). Any ideas as to why the below isn't working? NPE occurs when I call setAdapter because listView is null.
public class DisplayMessageActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
ArrayList<String> message = getArray();
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, message);
listView.setAdapter(adapter);
}; activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity" >
<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
<ListView android:id="@+id/listView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<TextView
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
</LinearLayout>
Upvotes: 1
Views: 72
Reputation: 20587
You are referencing R.layout.activity_display_message
when you call setContentView
meaning that (ListView) findViewById(R.id.listView);
will return null
Instead of setContentView(R.layout.activity_display_message)
call setContentView(R.layout.activity_main)
Upvotes: 2