昨天我在編程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);
}
}
你可以添加堆棧跟蹤嗎? – duffy356 2013-04-26 12:19:57
這是BluetoohService.java中的行號67?你確定OnCreate()中的daten String是否爲非null?嘗試記錄值可能。 – 2013-04-26 12:27:01
你不檢查'daten'是否爲'null'。 – m0skit0 2013-04-26 12:28:33