Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用setText()对TextView执行NullPointerException_Java_Android_Nullpointerexception_Textview - Fatal编程技术网

Java 使用setText()对TextView执行NullPointerException

Java 使用setText()对TextView执行NullPointerException,java,android,nullpointerexception,textview,Java,Android,Nullpointerexception,Textview,我对android编程相当陌生,遇到了我的老朋友NullPointerException 我已经做了很长一段时间了,但不知道怎么了 基本上,当我尝试调用任何.setText()方法时,都会给我一个NullPointerException,可能有人看到我在这里做错了什么…(尽管我尽可能接近示例) 另外,这是我的xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool

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

我已经做了很长一段时间了,但不知道怎么了

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

另外,这是我的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" />

说真的,我不知道如何修复它…

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

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

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

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

但无论如何,谢谢你的快速回答


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

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

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 ();
}
}

你能提供你的“updateLabels”方法吗?这就是logcat所指的,但由于您没有提供它,没有人能真正帮助您。我认为
teaching
null
,但我不能确定。lessonView.java的第59行是什么?可能
teaching
是null,在
updateLabels()中添加
如果(teaching!=null){…}
看看你是否还拥有与sanj无关的NPEThat。问题是他没有接受讲座。我想,他需要看一下意图被激发的代码。好吧,我就是这么做的,但那不起作用。为什么不去静态呢?因为此视图应根据要传递给它的对象显示不同的内容。
Intent intent = new Intent ();
intent.putExtra ("student",value);
Intent intent = getIntent();
if (intent.hasExtra("student")){
    lecture = (Lecture) intent.getParcelableExtra("student"); 
if (lecture !=null){
updateLabels ();
}
}