0

我正在使用android sdk中的蓝牙聊天示例来发送和接收许多活动中的字符串,而不是通过修改服务来接收主要活动,但是当我这样做时,应用程序强制关闭。我只是想在连接到主要活动中的设备后使用发送和接收字符串函数。笔记:

我正在使用具有默认类的 sdk 7 的蓝牙聊天示例: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);       
        }
    });

}
}

当我刚刚进入此活动时,应用程序将关闭。

对不起,我的代码中的间距问题,请尽可能多地帮助我,因为我没有从 stackoverflow 中的许多类似问题中受益。

这是我在 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

更新的新测试类:

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
        }
    });

}
}
4

1 回答 1

0

您正在将 Application 对象转换为您的 Application wrapper myapp。检查这个问题并接受答案。

对于您的情况,您可以执行以下操作:

  public class MyApplication extends Application {
    private static MyApplication singleton;

    private BluetoothChatService mBluetoothConnectedThread;

    // Returns the application instance
    public static MyApplication getInstance() {
        return singleton;
    }

    public BluetoothChatService getBluetoothConnectedThread() {
        return mBluetoothConnectedThread;
    }

    public void setBluetoothConnectedThread(
            BluetoothChatService mBluetoothConnectedThread) {
        this.mBluetoothConnectedThread = mBluetoothConnectedThread;
    }

    public final void onCreate() {
        super.onCreate();
        singleton = this;
    }
}

更新:

而不是调用:

final myapp a=(myapp)getApplication();
a.setBluetoothConnectedThread(myBluetoothConnectedThread);

称呼

MyApplication.getInstance().setBluetoothConnectedThread(myBluetoothConnectedThread);

在清单文件中更改:

<application android:label="@string/app_name"
         android:icon="@drawable/app_icon" >
         <application android:name="myapp" />

<application android:label="@string/app_name"
             android:icon="@drawable/app_icon" >
             <application android:name="com.example.android.MyApplication" />
于 2013-04-01T15:43:34.700 回答