setContentView上的Android RuntimeException

时间:2014-01-18 13:35:48

标签: android button nullpointerexception runtimeexception

我正在构建一个Android应用程序,在设置我的settingActivity时,我收到此错误:

当我开始活动时会发生这种情况。在我移动setContentView(R.layout.activity_setting)之前;在onCreate()函数的开头,应用程序在b.setOnCLickListener()处抛出了NullPointerException。 现在它在setContentView()中抛出一个RuntimeException;我该如何解决这个问题?

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代码:

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>

1 个答案:

答案 0 :(得分:0)

PreferenceActivity是一个ListActivityListActivity需要一个ListView ID为@android:id/list的布局。这也显示在您发布的例外中:

  

您的内容必须包含一个ListView,其id属性为'android.R.id.list'

现在,您的代码似乎不是常规Android偏好设置活动的代码。因此,您应该将extends PreferencesActivity更改为extends Activity