サービスを変更することにより、メインのアクティビティではなく、多くのアクティビティで文字列を送受信するために、Android SDK の Bluetooth チャット サンプルを使用していますが、それを行うとアプリケーションが強制終了します。メインアクティビティでデバイスに接続した後、文字列関数の送受信を操作したいだけです。ノート:
私は、デフォルトのクラスを持つSDK 7のBluetoothチャットサンプルを使用しています:Bluetoothchat、BluetoothchatService、Devicelistactivityこれはサービスコードです:
class myapp extends Application
{
private BluetoothChatService mBluetoothConnectedThread;
@Override
public void onCreate() {
super.onCreate();
}
public BluetoothChatService getBluetoothConnectedThread() {
return mBluetoothConnectedThread;
}
public void setBluetoothConnectedThread(BluetoothChatService mBluetoothConnectedThread) {
this.mBluetoothConnectedThread = mBluetoothConnectedThread;
}
}
そして、それは私がそれに文字列を送信しようとしている活動です:
public class newtest extends Activity {
private BluetoothChatService myBluetoothConnectedThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newtest);
Button got=(Button)findViewById(R.id.button1);
final myapp a=(myapp)getApplication();
a.setBluetoothConnectedThread(myBluetoothConnectedThread);
final byte[] d=new byte[]{127,-128,0};
got.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a.getBluetoothConnectedThread().write(d);
}
});
}
}
このアクティビティに入ると、アプリケーションの力が閉じます。
私のコードでスペースの問題が発生して申し訳ありません。スタックオーバーフローの多くの同様の質問から利益を得られなかったため、可能な限り助けてください。
そして、ここに私がlogcatで得たものがあります:
04-01 15:09:45.120: E/AndroidRuntime(16602): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.BluetoothChat/com.example.android.BluetoothChat.newtest}: java.lang.ClassCastException: android.app.Application
04-01 15:09:45.120: E/AndroidRuntime(16602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1769)
04-01 15:09:45.120: E/AndroidRuntime(16602): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1786)
04-01 15:09:45.120: E/AndroidRuntime(16602): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
04-01 15:09:45.120: E/AndroidRuntime(16602): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
04-01 15:09:45.120: E/AndroidRuntime(16602): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 15:09:45.120: E/AndroidRuntime(16602): at android.os.Looper.loop(Looper.java:130)
04-01 15:09:45.120: E/AndroidRuntime(16602): at android.app.ActivityThread.main(ActivityThread.java:3892)
04-01 15:09:45.120: E/AndroidRuntime(16602): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 15:09:45.120: E/AndroidRuntime(16602): at java.lang.reflect.Method.invoke(Method.java:507)
04-01 15:09:45.120: E/AndroidRuntime(16602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
04-01 15:09:45.120: E/AndroidRuntime(16602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
04-01 15:09:45.120: E/AndroidRuntime(16602): at dalvik.system.NativeStart.main(Native Method)
04-01 15:09:45.120: E/AndroidRuntime(16602): Caused by: java.lang.ClassCastException: android.app.Application
04-01 15:09:45.120: E/AndroidRuntime(16602): at com.example.android.BluetoothChat.newtest.onCreate(newtest.java:18)
04-01 15:09:45.120: E/AndroidRuntime(16602): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 15:09:45.120: E/AndroidRuntime(16602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1733)
04-01 15:09:45.120: E/AndroidRuntime(16602): ... 11 more
私のマニフェスト:
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.BluetoothChat"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk minSdkVersion="6" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application android:label="@string/app_name"
android:icon="@drawable/app_icon" >
<application android:name="myapp" />
<activity android:name=".BluetoothChat"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".t2"/>
<activity android:name=".newtest"/>
<activity android:name=".dup"/>
<activity android:name=".DeviceListActivity"
android:label="@string/select_device"
android:theme="@android:style/Theme.Dialog"
android:configChanges="orientation|keyboardHidden" />
</application>
</manifest>
新しいログキャット
04-01 19:24:52.199: E/AndroidRuntime(29443): FATAL EXCEPTION: main
04-01 19:24:52.199: E/AndroidRuntime(29443): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.android.BluetoothChat/com.example.android.BluetoothChat.newtest}: java.lang.NullPointerException
04-01 19:24:52.199: E/AndroidRuntime(29443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1769)
04-01 19:24:52.199: E/AndroidRuntime(29443): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1786)
04-01 19:24:52.199: E/AndroidRuntime(29443): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
04-01 19:24:52.199: E/AndroidRuntime(29443): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
04-01 19:24:52.199: E/AndroidRuntime(29443): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 19:24:52.199: E/AndroidRuntime(29443): at android.os.Looper.loop(Looper.java:130)
04-01 19:24:52.199: E/AndroidRuntime(29443): at android.app.ActivityThread.main(ActivityThread.java:3892)
04-01 19:24:52.199: E/AndroidRuntime(29443): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 19:24:52.199: E/AndroidRuntime(29443): at java.lang.reflect.Method.invoke(Method.java:507)
04-01 19:24:52.199: E/AndroidRuntime(29443): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
04-01 19:24:52.199: E/AndroidRuntime(29443): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
04-01 19:24:52.199: E/AndroidRuntime(29443): at dalvik.system.NativeStart.main(Native Method)
04-01 19:24:52.199: E/AndroidRuntime(29443): Caused by: java.lang.NullPointerException
04-01 19:24:52.199: E/AndroidRuntime(29443): at com.example.android.BluetoothChat.newtest.onCreate(newtest.java:23)
04-01 19:24:52.199: E/AndroidRuntime(29443): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 19:24:52.199: E/AndroidRuntime(29443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1733)
04-01 19:24:52.199: E/AndroidRuntime(29443): ... 11 more
更新された newtest クラス:
package com.example.android.BluetoothChat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class newtest extends Activity {
private BluetoothChatService myBluetoothConnectedThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newtest);
Button got=(Button)findViewById(R.id.button1);
MyApplication.getInstance().setBluetoothConnectedThread(myBluetoothConnectedThread);
final byte[] d=new byte[]{127,-128,0};
got.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myBluetoothConnectedThread.write(d);
// TODO Auto-generated method stub
}
});
}
}