2013-10-18 40 views
-2

编辑:对不起,我想通了。数据实际上是NULL。愚蠢的错误,应该在发布前正确调试。道歉。setText与NullPointerException失败

我知道这个问题已经被问了很多之前,但我仍然无法解决我的问题。

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

尝试创建textview内的oncreate实例,然后使用asynctask里面的 – SathishKumar

+0

你可以调试并找出数据是空的还是什么? –

+0

你确定数据不为空? – Blackbelt

回答

3

public byte[] data;声明后你还没有初始化它。 data为空。

1

请尝试以下操作。

byte[] data = null; 
String s = new String(data); 
text.setText(s); 
0

对不起,我想通了。其实是NULL,它是data。愚蠢的错误,应该在发布前正确调试。道歉。