-1

我有一个非常烦人的错误,我无法弄清楚是什么原因造成的。它看到我在尝试从另一个具有 Intent 的活动启动活动时遇到空指针异常。我尝试了我能想到的所有可能的解决方案,检查了数百次语法,尝试在新布局之前使用主布局设置内容视图..什么都没有!我在这里需要一些紧急帮助。我在清单中声明了所有内容,以及布局 xml 文件。问题依赖于由主要活动调用的 ShowNoteActivity(来自 onItemClick() 方法):我从 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);
    }

}

这是日志猫

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

吨>

这不是数据库问题或类似问题,即使我尝试将“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>
4

2 回答 2

1

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

setContentView(R.layout.activity_show_note);
TextView textView = (TextView)findViewById(R.id.show_note);
textView.setText(text);
于 2013-02-19T03:28:55.130 回答
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");
}
于 2013-02-19T03:58:36.257 回答