string - android bluetooth chat force closes when trying to run it as service -


i'm using bluetooth chat sample in android sdk send , recieve strings in many activites rather main activity modifying service,but when application force closes. want work sending , recieving string functions after connecting device in main activity. note:

i'm using bluetooth chat sample of sdk 7 has default classes: bluetoothchat,bluetoothchatservice,devicelistactivity 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 activty i'm trying send 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 enter activity application force closes.

sorry spacing problems in code, please me as possible because didn't benifit many similar questions in stackoverflow.

and here in logcat:

04-01 15:09:45.120: e/androidruntime(16602): java.lang.runtimeexception: unable 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):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1769) 04-01 15:09:45.120: e/androidruntime(16602):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:1786) 04-01 15:09:45.120: e/androidruntime(16602):    @ android.app.activitythread.access$1500(activitythread.java:121) 04-01 15:09:45.120: e/androidruntime(16602):    @ android.app.activitythread$h.handlemessage(activitythread.java:961) 04-01 15:09:45.120: e/androidruntime(16602):    @ android.os.handler.dispatchmessage(handler.java:99) 04-01 15:09:45.120: e/androidruntime(16602):    @ android.os.looper.loop(looper.java:130) 04-01 15:09:45.120: e/androidruntime(16602):    @ android.app.activitythread.main(activitythread.java:3892) 04-01 15:09:45.120: e/androidruntime(16602):    @ java.lang.reflect.method.invokenative(native method) 04-01 15:09:45.120: e/androidruntime(16602):    @ java.lang.reflect.method.invoke(method.java:507) 04-01 15:09:45.120: e/androidruntime(16602):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886) 04-01 15:09:45.120: e/androidruntime(16602):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:642) 04-01 15:09:45.120: e/androidruntime(16602):    @ 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):    @ com.example.android.bluetoothchat.newtest.oncreate(newtest.java:18) 04-01 15:09:45.120: e/androidruntime(16602):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 04-01 15:09:45.120: e/androidruntime(16602):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1733) 04-01 15:09:45.120: e/androidruntime(16602):    ... 11 more 

my manifest:

 licensed under apache license, version 2.0 (the "license");  may not use file except in compliance license.  may obtain copy of license @        http://www.apache.org/licenses/license-2.0   unless required applicable law or agreed in writing, software  distributed under license distributed on "as is" basis,  without warranties or conditions of kind, either express or implied.  see license specific language governing permissions ,  limitations under 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 start   activity componentinfo{com.example.android.bluetoothchat/com.example.android.bluetoothchat.newtest}:  java.lang.nullpointerexception 04-01 19:24:52.199: e/androidruntime(29443):    @     android.app.activitythread.performlaunchactivity(activitythread.java:1769) 04-01 19:24:52.199: e/androidruntime(29443):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:1786) 04-01 19:24:52.199: e/androidruntime(29443):    @ android.app.activitythread.access$1500(activitythread.java:121) 04-01 19:24:52.199: e/androidruntime(29443):    @ android.app.activitythread$h.handlemessage(activitythread.java:961) 04-01 19:24:52.199: e/androidruntime(29443):    @ android.os.handler.dispatchmessage(handler.java:99) 04-01 19:24:52.199: e/androidruntime(29443):    @ android.os.looper.loop(looper.java:130) 04-01 19:24:52.199: e/androidruntime(29443):    @ android.app.activitythread.main(activitythread.java:3892) 04-01 19:24:52.199: e/androidruntime(29443):    @ java.lang.reflect.method.invokenative(native method) 04-01 19:24:52.199: e/androidruntime(29443):    @ java.lang.reflect.method.invoke(method.java:507) 04-01 19:24:52.199: e/androidruntime(29443):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886) 04-01 19:24:52.199: e/androidruntime(29443):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:642) 04-01 19:24:52.199: e/androidruntime(29443):    @ 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):    @  com.example.android.bluetoothchat.newtest.oncreate(newtest.java:23) 04-01 19:24:52.199: e/androidruntime(29443):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 04-01 19:24:52.199: e/androidruntime(29443):    @  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         }     });  } } 

you casting application object application wrapper myapp. check this question , accepted answer.

for case can this:

  public class myapplication extends application {     private static myapplication singleton;      private bluetoothchatservice mbluetoothconnectedthread;      // returns 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;     } } 

update:

instead of calling:

final myapp a=(myapp)getapplication(); a.setbluetoothconnectedthread(mybluetoothconnectedthread); 

call

myapplication.getinstance().setbluetoothconnectedthread(mybluetoothconnectedthread); 

in manifest file change from:

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

to

<application android:label="@string/app_name"              android:icon="@drawable/app_icon" >              <application android:name="com.example.android.myapplication" /> 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -