在Android上使用TextView的空指针异常没有明显的原因

时间:2013-02-19 03:24:12

标签: java android android-activity nullpointerexception

我有一个非常烦人的错误,我无法弄清楚是什么导致它。在尝试使用Intent从另一个活动启动一个活动时,我发现我得到一个空指针异常。我尝试了所有可能的解决方案,我可以想到,检查语法houndreds次,尝试在新布局之前使用主布局setContentView ..没有!  我需要一些紧急帮助。我在清单中声明了所有内容,并且布局了xml文件。问题依赖于主要活动(来自onItemClick()方法)调用的ShowNoteActivity:我从logcat和一堆其他错误中得到nullpointerexception,我不知道它们是什么。

无论如何,这是相关的代码,告诉我你是否需要更多。

public class MainActivity extends Activity implements OnClickListener ,OnItemClickListener{

    public final static String EXTRA_MESSAGE = "assaf.notepad.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DatabaseManager db = new DatabaseManager(this);
        String[]notes = db.listNotes();
        ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,notes);
        ListView lv = (ListView)findViewById(R.id.notes_list);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        ImageButton addNoteBtn = (ImageButton)findViewById(R.id.add_note);
        addNoteBtn.setOnClickListener(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.activity_main, menu);
        return true;
    }



    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, AddNoteActivity.class);
        startActivity(intent);
    }



    @Override
    public void onItemClick(AdapterView adapter, View view, int position, long id) {
        DatabaseManager db = new DatabaseManager(this);
        String[]notesContent = db.getNoteContent();
        Intent intent = new Intent(this,ShowNoteActivity.class);
        intent.putExtra(EXTRA_MESSAGE, notesContent[position]);
        startActivity(intent);



    }



}

这是有问题的活动!!

public class ShowNoteActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String text = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView textView = (TextView)findViewById(R.id.show_note);
        textView.setText(text);
        setContentView(R.layout.activity_show_note);
    }

}

这是logcat

02-19 03:18:07.861: E/AndroidRuntime(829): FATAL EXCEPTION: main
02-19 03:18:07.861: E/AndroidRuntime(829): java.lang.RuntimeException: Unable to start activity ComponentInfo{assaf.notepad/assaf.notepad.ShowNoteActivity}: java.lang.NullPointerException
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.os.Looper.loop(Looper.java:137)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-19 03:18:07.861: E/AndroidRuntime(829):  at java.lang.reflect.Method.invokeNative(Native Method)
02-19 03:18:07.861: E/AndroidRuntime(829):  at java.lang.reflect.Method.invoke(Method.java:511)
02-19 03:18:07.861: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-19 03:18:07.861: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-19 03:18:07.861: E/AndroidRuntime(829):  at dalvik.system.NativeStart.main(Native Method)
02-19 03:18:07.861: E/AndroidRuntime(829): Caused by: java.lang.NullPointerException
02-19 03:18:07.861: E/AndroidRuntime(829):  at assaf.notepad.ShowNoteActivity.onCreate(ShowNoteActivity.java:19)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.Activity.performCreate(Activity.java:5104)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-19 03:18:07.861: E/AndroidRuntime(829):  ... 11 more

相关的xml,“ShowNoteActivity”xml,带有问题的textView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/board"
        android:scaleType="fitXY"/>


    <Button
        android:id="@+id/edit_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/edit_btn"
        android:textColor="@color/white"/>

    <Button
        android:id="@+id/delete_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/edit_btn"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:text="@string/delete_btn"
        android:textColor="@color/white"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/paper"
        android:layout_below="@id/edit_btn"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="30dp"/>

    <TextView
        android:id="@+id/show_note"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/edit_btn"
        android:layout_marginTop="110dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="45dp"
        android:layout_marginBottom="80dp"
        android:typeface="sans"
        android:textSize="10sp"/>




</RelativeLayou

T&GT;

这不是数据库问题或类似问题,即使我尝试将“hello world”字符串传递给新活动,问题仍然存在。调用新活动时程序总是崩溃。

这是xml清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="assaf.notepad"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="assaf.notepad.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="assaf.notepad.AddNoteActivity">

        </activity>
        <activity
            android:name="assaf.notepad.ShowNoteActivity">

        </activity>



    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

问题是您在ShowNoteActivity中调用setContentView之前尝试访问TextView。你的代码应该是:

setContentView(R.layout.activity_show_note);
TextView textView = (TextView)findViewById(R.id.show_note);
textView.setText(text);

答案 1 :(得分:0)

setContentView(R.layout.activity_show_note);    
Intent intent = getIntent();
String text = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

TextView textView = (TextView)findViewById(R.id.show_note);
if(textView!=null && text!=null)
{
    Log.d("Mytag","Everything is right");
    textView.setText(text);
}
else
{
if(textView==null)
    Log.d("Mytag","Textview is null");
else
    Log.d("Mytag","Text is null");
}