使用setText()在TextView上使用NullPointerException

时间:2015-04-24 23:43:54

标签: java android nullpointerexception textview

我对Android编程很新,我遇到了我的老朋友NullPointerException ...

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

当我尝试调用任何NullPointerException方法时,

基本上会给我一个.setText(),也许有人会看到我在这里做错了什么...(尽管我试图跟随示例一样接近可能的)

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" />

和我得到的logcat:

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)

严肃地说,我对如何修复它没有想法......

3 个答案:

答案 0 :(得分:0)

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

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

然后测试讲座是否像拉姆先前写的那样无效。

答案 1 :(得分:0)

好的,谢谢你的建议,但我会走全球路线。我将创建一个我的数组的静态版本(我想从中通过Intent传递一个对象),然后传递一个带有coordiantes的int []数组并在新视图中调用它。

但是谢谢你的快速回答!

(我是stackoverflow的新手,有没有办法将问题标记为“已解决”或“不再需要解决”?)

答案 2 :(得分:0)

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

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

以意图开始活动 在你的 lessonViewActivity检查意图,如

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