我昨天在编写一个android应用程序时遇到了困难。首先,我通过发送广播意图并对此做出反应来解决我的问题。但是,请您帮我理解这个问题:
我尝试更新一个 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));
}
(String daten) - 这个字符串来自一个服务,在蓝牙套接字上监听。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);
}
};
}
(你可以看到,我通过使用广播解决了我的问题......)调试时,当处理“txt.setText(Daten)”时,我看到一个 NullPointerException。
我试着做
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)
抱歉,似乎我真的无法正确表达...这里是 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);
}
}