NullPointerException on TextView using setText()



我对安卓编程相当陌生,NullPointerException遇到了我的老朋友......

我已经在上面呆了一段时间了,但无法弄清楚出了什么问题。

基本上,当我尝试调用任何.setText()方法时,会给我一个NullPointerException,也许有人看到我在这里做错了什么......(尽管我试图尽可能接近示例)

public class lessonView extends Activity {
TextView adressOfLecture;
TextView lecturer;
TextView lesson;
Lecture lecture;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lesson_view);
    Bundle data = getIntent().getExtras();
    lecture = (Lecture) data.getParcelable("student");
    adressOfLecture = (TextView)findViewById(R.id.lectureViewAdressLabel);
    lecturer = (TextView)findViewById(R.id.lectureViewLecturerLabel);
    lesson = (TextView)findViewById(R.id.lectureViewTitle);
    updateLabels();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_lesson_view, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
private void updateLabels(){
    adressOfLecture.setText(lecture.getRoom());
    lecturer.setText(lecture.getTutor());
    lesson.setText(lecture.getName());
}
}

另外,这是我的XML文件:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.coronarip7.app.stupla.lessonView">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="(ort)"
    android:id="@+id/lectureViewAdressLabel"
    android:layout_centerVertical="true"
    android:layout_toStartOf="@+id/lectureViewTitle"
    android:layout_marginRight="86dp" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dozent"
    android:id="@+id/lectureViewLecturerLabel"
    android:layout_toEndOf="@+id/lectureViewTitle"
    android:layout_alignTop="@+id/lectureViewAdressLabel"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="27dp" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lectureViewTitle"
    android:gravity="center_horizontal"
    android:text="test"
    android:textSize="40dp"
    android:textStyle="bold"
    android:padding="10dp"
    android:layout_row="0"
    android:layout_column="0"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

和我得到的日志猫:

04-25 01:23:51.058  12236-12236/com.coronarip7.app.stupla E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.coronarip7.app.stupla, PID: 12236
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coronarip7.app.stupla/com.coronarip7.app.stupla.lessonView}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
        at android.app.ActivityThread.access$800(ActivityThread.java:145)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5081)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.coronarip7.app.stupla.lessonView.updateLabels(lessonView.java:59)
        at com.coronarip7.app.stupla.lessonView.onCreate(lessonView.java:31)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

说真的,我对如何解决它没有想法......

首先检查你是否正确地将额外的内容放在意图中。然后我会在你的onCreate方法中使用以下测试:

Intent intent = getIntent();
if (intent.hasExtra("student")){
    lecture = (Lecture) intent.getParcelableExtra("student");
}

然后测试讲座是否像拉米之前写的那样为空。

好的,伙计们,感谢您的建议,但我会走全球路线。我将创建数组的静态版本(我想通过 Intent 从中传递对象),只需传递一个带有坐标的 int[] 数组并在新视图中调用它。

但无论如何,感谢您的快速回答!

(我是堆栈溢出的新手,有没有办法将问题标记为"已解决"或"不再需要解决"?

不要在代码中使用静态对象,例如数组、对象,它们具有全局可用,您应该创建 tent 并将您的数据添加到您的 tent 中,例如并调用您的活动

Intent intent = new Intent ();
intent.putExtra ("student",value);

有意图地启动活动而在你的课程视图活动检查意图,如

Intent intent = getIntent();
if (intent.hasExtra("student")){
    lecture = (Lecture) intent.getParcelableExtra("student"); 
if (lecture !=null){
updateLabels ();
}
}

最新更新