0

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

}
}
Youssef Fotouh
  • 29
  • 1
  • 1
  • 5

1 Answers1

0

You are casting Application object to your Application wrapper myapp. Check this question and accepted answer.

For your case you can do something like this:

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

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" />
Community
  • 1
  • 1
Taras
  • 2,526
  • 3
  • 33
  • 63
  • i tried that but i still force closes, i will add the manifest in my question maybe i have something wrong – Youssef Fotouh Apr 01 '13 at 16:09
  • the logcat after i have made your edit now or early, if early then i have updated my question – Youssef Fotouh Apr 01 '13 at 16:12
  • @YoussefFotouh you need to set full name of your class, e.g. com.mypackage.myapp – Taras Apr 01 '13 at 16:15
  • i'm sorry i hope you are very patient with me because this involves myy graduation project.....i have added the class you wrote now in my project and i have added this line "MyApplication.getInstance();" in newtest activity class, now how can i use it?.....and sorry again what should be the exactly the name of the application in my manifest file please tell me exactly the name format....thank you – Youssef Fotouh Apr 01 '13 at 16:56
  • my package name is com.example.android.BluetoothChat – Youssef Fotouh Apr 01 '13 at 17:03
  • ok used MyApplication.getInstance().setBluetoothConnectedThread(myBluetoothConnectedThread); to call the application and i have edited my application name in the manifest file as you said.....but still my application force closes!!! and i have used "myBluetoothConnectedThread.write(d);" in the on click listner but the application force closes before even reaching the activity....... shall i send further information for my application... – Youssef Fotouh Apr 01 '13 at 17:17
  • @YoussefFotouh please, update output of your logcat. There must be another issue. – Taras Apr 01 '13 at 17:20
  • doesn't look much different for me..... have you tried to do this thing i'm trying to do before....do you know an easier way or something....is this way i'm doing logically correct in the first place!!! – Youssef Fotouh Apr 01 '13 at 17:33
  • i will add the new logcat anyway – Youssef Fotouh Apr 01 '13 at 17:34
  • You've got NullPointerException. Where is located MyApplication class? – Taras Apr 01 '13 at 17:43
  • no believe me i have checked now, i did exactly what you told i have commented all the previous errors in the newtest file i have private BluetoothChatService myBluetoothConnectedThread; defined globally and MyApplication.getInstance().setBluetoothConnectedThread(myBluetoothConnectedThread); inside the oncreate function and myBluetoothConnectedThread.write(d); inside the onclick listener function – Youssef Fotouh Apr 01 '13 at 17:46
  • @YoussefFotouh Oh, your myBluetoothConnectedThread variable is not initialized, when you pass it into MyApplication class. – Taras Apr 01 '13 at 17:49