2013-04-26 28 views
1

昨天我在編程android應用程序時被卡住了。 首先,我通過發送廣播意圖解決了我的問題,並對此做出了反應。但是,請你能不能幫我unterstand問題:嘗試在創建活動對象後在TextView上設置文本時出現異常

我嘗試更新的TextView:

public void setProgressBarState(String daten) { 
    try { 

    txt.setText(daten); 
    } 
    catch (Exception e) { 
    Log.d(getPackageName(), daten, e); 
    } 
    //progressBarHandler.sendMessage(progressBarHandler.obtainMessage(0, 0, 0,daten)); 
} 

(字符串回到名單Daten) - 該字符串從服務來了,在的BluetoothSocket至極聽。 MessageHandler的一個接收到的數據發送到setProgressBarState方法:

public void onCreate() { 
    dataHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      String daten=(String) msg.obj; 
      //Toast.makeText(BluetoothService.this, daten, Toast.LENGTH_SHORT).show(); 
      RemoteControl remote = new RemoteControl(); 
      remote.setProgressBarState(daten); 
      Intent intent = new Intent("DataAvialable"); 
      intent.putExtra("DATA", daten); 
      LocalBroadcastManager.getInstance(ctx).sendBroadcastSync(intent); 



     } 
    }; 



} 

(你可以看到,我解決我的問題,通過使用廣播...) 調試時,我看到一個NullPointerException異常時「txt.setText(回到名單Daten)」被處理。

我試圖做

TextView txt = (TextView) findViewById (R.id.TextView2); 

,但後來我得到的InvocationTargetException。

我認爲這是一件非常容易的事情,但對不起,我無法解決這個問題(僅限於我的廣播)...請任何人都可以告訴我爲什麼無法在TxextView中設置文本。 ..

對不起,我會回答任何問題,如果有什麼不明確的。

預先感謝您!

編輯:希望這有助於....

> 04-26 13:23:34.279: E/AndroidRuntime(9464): FATAL EXCEPTION: main 
04-26 13:23:34.279: E/AndroidRuntime(9464): java.lang.NullPointerException 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at android.content.ContextWrapper.getPackageName(ContextWrapper.java:123) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at com.example.bluecomm.RemoteControl.setProgressBarState(RemoteControl.java:117) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at com.example.bluecomm.BluetoothService$1.handleMessage(BluetoothService.java:67) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at android.os.Looper.loop(Looper.java:130) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) 
04-26 13:23:34.279: E/AndroidRuntime(9464):  at dalvik.system.NativeStart.main(Native Method) 

對不起,似乎IAM不是真的能夠framt吧... 這裏是XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".BlueCommMain" > 

<Button 
    android:id="@+id/vol_Mute" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/vol_Up" 
    android:layout_marginLeft="34dp" 
    android:text="@string/Vol_Mute_Button" /> 

<Button 
    android:id="@+id/vol_Up" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginRight="53dp" 
    android:layout_marginTop="69dp" 
    android:text="@string/Vol_Up_Button" /> 

<Button 
    android:id="@+id/vol_Down" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/vol_Up" 
    android:layout_below="@+id/vol_Mute" 
    android:text="@string/Vol_Down_Button" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/vol_Down" 
    android:layout_below="@+id/vol_Down" 
    android:layout_marginTop="46dp" 
    android:text="Aktuelle Lautstärke:" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="26dp" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/progressBar1" 
    android:layout_below="@+id/progressBar1" 
    android:layout_marginLeft="42dp" 
    android:layout_marginTop="20dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

的這是我的活動

ackage com.example.bluecomm; 

import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.os.Vibrator; 
import android.support.v4.content.LocalBroadcastManager; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ProgressBar; 
import android.widget.RemoteViews; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 

public class RemoteControl extends Activity { 
String mac = null; 
Context ctx; 
ProgressBar progress; 
Handler progressBarHandler; 
TextView txt; 
BroadcastReceiver mMessageReceiver; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.i(getPackageName(), "onCreate"); 
     setContentView(R.layout.volume_remote); 
     txt = (TextView) findViewById(R.id.textView2); 
     Intent intent = getIntent(); 
     Bundle daten = intent.getExtras(); 
     mac = daten.getString("MAC"); 
     //progress=new ProgressBar(RemoteControl.this); 
     progress=(ProgressBar)findViewById(R.id.progressBar1); 
     final Vibrator vib = (Vibrator) getSystemService(VIBRATOR_SERVICE); 






     Button btn_Vol_Up = (Button) findViewById (R.id.vol_Up); 
     btn_Vol_Up.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick (View v) { 
       Intent service = new Intent(v.getContext(), BluetoothService.class); 
       service.putExtra("MAC", mac); 
       service.putExtra("MESSAGE", "VOL_UP"); 

       vib.vibrate(30); 
       startService(service); 

      }}); 

     Button btn_Vol_Down = (Button) findViewById (R.id.vol_Down); 
     btn_Vol_Down.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick (View v) { 
       Intent service = new Intent(v.getContext(), BluetoothService.class); 
       service.putExtra("MAC", mac); 
       service.putExtra("MESSAGE", "VOL_DOWN"); 
       vib.vibrate(50); 
       startService(service); 
      }}); 

     Button btn_Mute = (Button) findViewById (R.id.vol_Mute); 
     btn_Mute.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick (View v) { 
       Intent service = new Intent(v.getContext(), BluetoothService.class); 
       service.putExtra("MAC", mac); 
       service.putExtra("MESSAGE", "MUTE"); 
       vib.vibrate(100); 
       startService(service); 
      }}); 




     mMessageReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
      Log.i(getPackageName(), "Got Intent"); 
      String volume=intent.getStringExtra("DATA"); 
      Double volume_dbl = Double.valueOf(volume); 
      volume_dbl=volume_dbl*100; 
      int volume_int=(int)Math.round(volume_dbl); 
      txt.setText(String.valueOf(volume_int)); 
      progress.setProgress(volume_int); 
      } 
     }; 


     LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, 
       new IntentFilter("DataAvialable")); 
    } 






    public void setProgressBarState(String daten) { 
     try { 

     txt.setText(daten); 
     } 
     catch (Exception e) { 
     Log.d(getPackageName(), daten, e); 
     } 
      } 

    @Override 
    public void onResume(){ 
     super.onResume(); 
     Log.i(getPackageName(), "onResume"); 

    } 






    @Override 
    public void onPause(){ 
     super.onPause(); 

     LocalBroadcastManager.getInstance(ctx).unregisterReceiver(mMessageReceiver); 
     Log.i(getPackageName(), "onPause"); 
     //stopService(service); 

    } 

    } 
+0

你可以添加堆棧跟蹤嗎? – duffy356 2013-04-26 12:19:57

+0

這是BluetoohService.java中的行號67?你確定OnCreate()中的daten String是否爲非null?嘗試記錄值可能。 – 2013-04-26 12:27:01

+0

你不檢查'daten'是否爲'null'。 – m0skit0 2013-04-26 12:28:33

回答

0

正如我所說,如果您正在嘗試查找具有不存在的給定ID的textview,則會發生InvocationTargetException。在佈局文件中,文本視圖ID爲「textView2」,並帶有一個小的「t」。但是您正在嘗試查找ID爲「TextView2」的textView。他們都是不同的ID。所以請嘗試使用以下內容:

TextView txt = (TextView) findViewById (R.id.textView2); 
txt.setText(daten); 

希望它有幫助。

更新

如果DATEN的setProgressBarState()函數內部的非空值,那麼問題可能是您正在創建您的處理程序中的「REMOTECONTROL」類的對象。如果你嘗試用普通的方式實例化一個Activity(MyActivity ma = new MyActivity();),那麼onCreate()方法將不會被調用。所以它可能無法找到TextView。

你可以嘗試以另一種方式完成你的任務。您可以將值保存在SharedPreference中,然後在用戶實例化活動時再次從SharedPreferences中獲取該值並將其顯示給用戶。

+0

我編輯我的方法公共無效setProgressBarState(字符串回到名單Daten){ \t \t \t TextView的\t TXT =(TextView的)findViewById(R.id.textView2); \t \t txt.setText(daten); \t \t \t},但得到java.lang.reflect.InvocationTargetException – 2013-04-26 12:53:19

+0

如果回到名單Daten作爲非空,那麼問題可能是正在創建中的「REMOTECONTROL」類的對象的setProgressBarState()函數中的數值您處理程序。如果你嘗試通過普通方式實例化一個Activity(MyActivity ma = new MyActivity();),那麼onCreate()方法將不會被調用。只有當您以意向啓動Activity時,纔會調用該方法。所以,它可能無法找到txt的TextView。你可能會嘗試並在setProgressBarState()中定義「TextView txt =(TextView)findViewById(R.id.textView2);」{} – 2013-04-26 13:00:54

+0

這意味着,如果我從一個Activity創建一個對象來調用方法setProgressBarState() ,「TextView txt =(TextView)findViewById(R.id.textView2);無法解析,因爲在這種情況下,Android沒有」鏈接「到XML ...並找不到textView2 ...等等 – 2013-04-26 13:07:41

相關問題