编辑:对不起伙计们,我想通了。事实上,数据是NULL。愚蠢的错误,应该在发布前正确调试。道歉。
我知道这个问题之前已被问过很多,但我仍然无法解决我的问题。
activity_abcd.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=".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));
}
}
}
我收到了text.setText("" + new String(data))
行的NullPointerException。
我无法弄清楚原因。我正在Android studio中开发,IDE在运行之前不会产生任何错误。
答案 0 :(得分:3)
public byte[] data;
声明后你还没有初始化它。 data
为空。
答案 1 :(得分:1)
尝试使用以下内容。
byte[] data = null;
String s = new String(data);
text.setText(s);
答案 2 :(得分:0)
对不起伙计们,我想通了。 data
实际上是NULL。愚蠢的错误,应该在发布前正确调试。道歉。