i'm using the bluetooth chat sample in the android sdk to send and recieve strings in many activites rather than the main activity by modifying a service,but when i do that the application force closes. i simply want to work with sending and recieving string functions after connecting to a device in the main activity. note:
i'm using bluetooth chat sample of sdk 7 which has default classes: Bluetoothchat,BluetoothchatService,Devicelistactivity this is the service code:
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;
}
}
and that's the activty i'm trying to send a string in it:
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);
}
});
}
}
when i just enter this activity the application force closes.
sorry for spacing problems in my code, please help me as much as possible because i didn't benifit from many similar questions in stackoverflow.
and here is what i get in the 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
my manifest:
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>
new logcat
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
updated newtest class:
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
}
});
}
}