简体   繁体   English

使用setText()在TextView上发生NullPointerException

[英]NullPointerException on TextView using setText()

I'm rather new to android programming, and am running into my old friend the NullPointerException ... 我是android编程的新手,并且遇到了我的老朋友NullPointerException ...

I've been on it for quite a while now, but can't figure out what is wrong. 我已经花了很长时间了,但无法弄清楚出什么问题了。

basicly gives me a NullPointerException when I try to call any .setText() methods, maybe someone sees what I'm doing wrong here...(though i tried to follow examples as close as possible) 当我尝试调用任何.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());
}

}

also, here's my xml file: 另外,这是我的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" />

and the logcat I'm getting: 和我得到的日志猫:

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)

seriously, I am out of ideas on how to fix it... 认真的说,我对如何解决它没有想法...

First check if you properly put the extra in the intent. 首先检查您是否正确放置了多余的内容。 Then I would use the following test in your onCreate method: 然后,我将在您的onCreate方法中使用以下测试:

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

Then test if lecture is null like Rami wrote earlier. 然后测试演讲是否为空,如Rami先前所写。

Ok guys, thanks for the suggestions, but I'll go the global route. 好的,谢谢您的建议,但我会走全球路线。 I'll create a static version of my array (from which i wanted to pass an object via the Intent), and just pass an int[] array with the coordiantes and call it in the new view. 我将创建一个数组的静态版本(我想通过该数组通过Intent传递对象),然后将带有coordiantes的int []数组传递给新视图。

But thanks for the quick answers anyways! 但是还是感谢您的快速解答!

(I'm new to stackoverflow, is there a way to mark a question as "solved", or "not-needed-to-be-solved-anymore"?) (我是stackoverflow的新手,有没有办法将问题标记为“已解决”或“不再需要解决”?)

Do not use static objects in your code like arrays, objects, they have globally available, you should create intent and add your data into your intent like and call your activity 不要在代码中使用静态对象,例如数组,对象,它们在全局范围内可用,您应该创建意图并将数据添加到意图中,然后调用活动

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

start activity with intent And in your lessonViewActivity check for intent like 以意图开始活动,并在您的LessonViewActivity中检查意图

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM