2013-10-18 115 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。愚蠢的錯誤,應該在發佈前正確調試。道歉。

相關問題