简体   繁体   中英

Android Resources$NotFoundException: Resource ID #0x0

Right now I am trying to create an activity with an EditText and a button, and below that a listview were I can Dynamically add strings to the list. I'm getting errors trying to mock up my activity.

Here is my main activity.

public class MainActivity extends Activity {

private Connection serverConnection;
private ArrayList<String> listItems = new ArrayList<String>();
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ListView listview = (ListView) findViewById(R.id.list);
    adapter = new ArrayAdapter<String>(MainActivity.this, 0, listItems);
    listview.setAdapter(adapter);
    serverConnection = new Connection(MainActivity.this);

}



@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;
}

public void displayMessage(String string) {
     listItems.add(string);
     adapter.notifyDataSetChanged();

}

}

Here is the xml for the main activity.

<RelativeLayout 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"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/message_entry"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/list"
    android:layout_toLeftOf="@+id/send_button"
    android:layout_weight="1"
    android:hint="@string/enter_message" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/message_entry"
        android:paddingTop="10dp" />

    <Button
        android:id="@+id/send_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/message_entry"
        android:layout_alignRight="@+id/list"
        android:layout_alignTop="@+id/message_entry" />

</RelativeLayout>

Here is my other class that I'm going to use to for connecting to a socket.

public class Connection extends Thread {

private Socket client; 
private ObjectOutputStream output;
private ObjectInputStream input;

public Connection(MainActivity mainActivity) {


    mainActivity.displayMessage("Test1");
    mainActivity.displayMessage("Test2");       
    mainActivity.displayMessage("Test3");
    mainActivity.displayMessage("Test4");
    mainActivity.displayMessage("Test5");


}
}

This line gives you an error

adapter = new ArrayAdapter<String>(MainActivity.this, 0, listItems);

The second parameter accepts layout resourceID where you can populate the value (String) to the layout.

You can use the layout of android framework which is android.R.layout.simple_list_item_1

adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, listItems);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM