Question

I am building an app for android, and while setting up my settingActivity and i get this error:

This happens when I start the activity. Before I moved the setContentView(R.layout.activity_setting); to the start of the onCreate() function, the application threw a NullPointerException at b.setOnCLickListener(). Now it throws a RuntimeException at the setContentView(); How do I resolve this?

01-18 14:28:59.116: E/AndroidRuntime(9463): java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.yteditors.london2013/tk.yteditors.london2013.SettingActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is    'android.R.id.list'
01-18 14:28:59.116: E/AndroidRuntime(9463):     
at tk.yteditors.london2013.SettingActivity.onCreate(SettingActivity.java:27)

Java code:

package tk.yteditors.london2013;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SettingActivity extends PreferenceActivity{

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupActionBar();

    setContentView(R.layout.activity_setting);

    Button b = (Button) findViewById(R.id.sendAnswers);
    b.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View pView) {
            sendAnswers();
        }
    });


}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case R.id.MenuSettings:
        //boo
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

public void sendAnswers(){
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.activity_confirm_dialog);
    dialog.setTitle("Antwoorden verzenden?");
    ((Button) dialog.findViewById(R.id.dialogNo)).setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            dialog.dismiss();
        }
    });
    ((Button) dialog.findViewById(R.id.dialogYes)).setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            EditText edit = (EditText) dialog.findViewById(R.id.sendAnswers);
            packHTML(edit.getText().toString());
        }
    });


}

public void packHTML(String name){
    try {
        String fileDir = "/sdcard/LondonAnswers";
        File dir = new File(fileDir);
        dir.mkdirs();
        String fileName = name +new Random().nextLong() +".html";
        File file = new File(dir, fileName);
        FileWriter fw = new FileWriter(file, false);
        fw.append("<html>\n");
        fw.append("<head><title>" +name +"'s antwoorden</title></head>\n");
        fw.append("<body>\n");
        fw.append("<h1>" +name +"'s antwoorden</h1>\n");
        fw.append("</body>\n");
        fw.append("</html>");

        fw.close();

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("webAdress", "file://" +fileDir +fileName);
        startActivity(intent);
    } catch (IOException e) {
        Toast t = new Toast(this);
        t.setText("Er is iets mis gegaan tijdens het maken van het bestand");
        t.setDuration(Toast.LENGTH_SHORT);
        t.show();
        e.printStackTrace();
    }
}
}

xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Antwoorden"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/sendAnswers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Verzend antwoorden" />

    <Button
        android:id="@+id/removeAnswers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Verwijder antwoorden" />

    <Button
        android:id="@+id/answersNYI"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="NYI" />

</LinearLayout>

</ScrollView>
Was it helpful?

Solution

PreferenceActivity is-a ListActivity, and a ListActivity requires a layout which has a ListView with id @android:id/list. This is also shown in the exception you posted:

Your content must have a ListView whose id attribute is 'android.R.id.list'

Now, it seems that your code doesn't appear to be that of a regular Android preferences activity. Therefore you should change the extends PreferencesActivity to just extends Activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top