Java setText因NullPointerException而失败
编辑:对不起,伙计们,我知道了。数据实际上是空的。愚蠢的错误,应该在发布前正确调试。道歉。 我知道这个问题以前被问过很多次,但我仍然无法解决我的问题 活动_abcd.xmlJava setText因NullPointerException而失败,java,android,Java,Android,编辑:对不起,伙计们,我知道了。数据实际上是空的。愚蠢的错误,应该在发布前正确调试。道歉。 我知道这个问题以前被问过很多次,但我仍然无法解决我的问题 活动_abcd.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pa
<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=".ActivityAbcd">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="nothing set"
android:id="@+id/outputtv" />
</ScrollView>
</RelativeLayout>
activityAbcd.java
package com.example.myfirstapp;
public class ActivityAbcd extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//STUFF
setContentView(R.layout.activity_abcd);
new SocketTask().execute();
}
public final class SocketTask extends AsyncTask<Void, Void, Void> {
public byte[] data;
@Override
protected Void doInBackground(Void... voids) {
//STUFF involving data
}
@Override
protected void onPostExecute(Void voids) {
TextView text = (TextView) findViewById(R.id.outputtv);
text.setText("" + new String(data));
}
}
}
package com.example.myfirstapp;
公共类活动ABCD扩展活动{
@SuppressLint(“新API”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//东西
setContentView(R.layout.activity\u abcd);
新建SocketTask().execute();
}
公共最终类SocketTask扩展异步任务{
公共字节[]数据;
@凌驾
受保护的空位背景(空位…空位){
//涉及数据的东西
}
@凌驾
后期执行时受保护的无效(无效无效){
TextView text=(TextView)findViewById(R.id.outputtv);
text.setText(“+新字符串(数据));
}
}
}
我得到了text.setText(“+newstring(data))
行的NullPointerException
我不明白为什么。我在Android studio中开发,IDE在运行前不会产生任何错误。
public byte[]数据代码>声明后,您尚未初始化它<代码>数据
为空。请尝试以下操作
byte[] data = null;
String s = new String(data);
text.setText(s);
对不起,伙计们,我知道了<代码>数据实际上是空的。愚蠢的错误,应该在发布前正确调试。抱歉。尝试在oncreate内部为textview创建实例,然后在AsyncTask内部使用该实例。您是否可以调试并找出数据是否为空?是否确定数据不为空?尝试在
日志中打印数据的值