昨日、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) - この String は、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);
}
};
}
(ご覧のとおり、Broadcast を使用して問題を解決しました...) デバッグ時に、"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)
申し訳ありませんが、正しく 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);
}
}