블루투스 샘플소스를 그대로 응용해서 사용하고있는데....
문제가 되는 부분은 MainActivity.sendMessage("0x0234"); 입니다.
간략하게 말씀드리면 액티비티가 2개가 있는데 MainActivity 액티비티 SubActivity 액티비티가 있습니다.
MainActivity 액티비티에 블루투스 통신관련 핸들러와 sendMessage 메서드가 구현되어있습니다.
그리고 SubActivity 액티비티에서 아래와 같이 선안한다음 sendMessage메서드를 호출하게되면 어플이 죽는 현상이 발생합니다.
아주 간단한 문제인것같은데 찾지를 못하고 있습니다. 고수님 도와주세요..
private static MainActivity mMainActivity = new MainActivity ();
MainActivity.sendMessage("0x0234"); //문제가 되는 메서드임
public class MainActivity extends Activity
{
public void sendMessage(String message) {
// Check that we're actually connected before trying anything
if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT)
.show();
return;
}
// Check that there's actually something to send
if (message.length() > 0) {
// Get the message bytes and tell the BluetoothChatService to write
mChatService.write(hexToByteArray(message));
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
// mOutEditText.setText(mOutStringBuffer);
}
}
private final Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1)
{
case BluetoothChatService.STATE_CONNECTED:
// setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
// mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
// setStatus(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
// setStatus(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
//mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
//construct a string from the valid bytes in the buffer
BT_RxData(msg.arg1, readBuf);
break;
case MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};
}
public class SubActivity extends Activity
{
private static MainActivity mMainActivity = new MainActivity ();
MainActivity.sendMessage("0x0234");
}
아래는 에러 로그입니다.> 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): FATAL EXCEPTION: main 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.kr.VIDGON/co.kr.VIDGON.DriveInfo}: java.lang.NullPointerException 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at android.os.Handler.dispatchMessage(Handler.java:99) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at android.os.Looper.loop(Looper.java:123) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at android.app.ActivityThread.main(ActivityThread.java:3691) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at java.lang.reflect.Method.invokeNative(Native Method) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at java.lang.reflect.Method.invoke(Method.java:507) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at dalvik.system.NativeStart.main(Native Method) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): Caused by: java.lang.NullPointerException 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at co.kr.VIDGON.VIDGON.sendMessage(VIDGON.java:307) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at co.kr.VIDGON.DriveInfo.setList(DriveInfo.java:84) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at co.kr.VIDGON.DriveInfo.Driveinfo_initProce(DriveInfo.java:46) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at co.kr.VIDGON.DriveInfo.onCreate(DriveInfo.java:31) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 09-15 11:26:54.060: ERROR/AndroidRuntime(17039): ... 11 more 09-15 11:26:54.065: WARN/ActivityManager(2999): Force finishing activity co.kr.VIDGON/.DriveInfo 09-15 11:26:54.065: WARN/ActivityManager(2999): Force finishing activity co.kr.VIDGON/.VIDGON 09-15 11:26:54.070: ERROR/(2999): Dumpstate > /data/log/dumpstate_app_error 09-15 11:26:54.075: INFO/dumpstate(17117): begin 09-15 11:26:54.515: DEBUG/PowerManagerService(2999): onSensorChanged: light value: 1000 09-15 11:26:54.565: WARN/ActivityManager(2999): Activity pause timeout for HistoryRecord{40befcd0 co.kr.VIDGON/.DriveInfo} 09-15 11:26:55.400: ERROR/lights(2999): write_int: path /sys/devices/virtual/misc/melfas_touchkey/brightness, value 2 09-15 11:26:55.400: WARN/PowerManagerService(2999): Timer 0x7->0x3|0x0 09-15 11:26:55.400: INFO/PowerManagerService(2999): Ulight 7->3|0 09-15 11:26:55.400: DEBUG/PowerManagerService(2999): setLightBrightness : mButtonLight : 0 09-15 11:26:57.505: DEBUG/VoldCmdListener(2583): asec list 09-15 11:26:58.445: INFO/dumpstate(17117): done 09-15 11:26:58.530: INFO/OrientationDebug(2999): [pwm] in updateOrientationListenerLp() 09-15 11:26:58.530: VERBOSE/OrientationDebug(2999): in updateOrientationListenerLp(), Screen status=true, current orientation=1, SensorEnabled=false 09-15 11:26:58.530: INFO/OrientationDebug(2999): [pwm] needSensorRunningLp(), return false #3 09-15 11:26:58.535: INFO/Launcher(3169): onResume(). mIsNewIntent : false screenOff: false 09-15 11:26:58.540: ERROR/WallpaperService(2999): setDimensionHints SET w=-1 09-15 11:26:58.565: ERROR/Launcher(3169): MTP-LAUNCHER: media scanning not yet finished . 09-15 11:26:58.580: ERROR/WallpaperService(2999): setDimensionHints ! w=533
sendmessage 메소드를 static으로 선언을 해서 subactivity에서 mainactivity를 생성하는게 아니라 바로 사용해보시는건 어떨까요?
MainActivity.sendmessage()
이런식으로요




블루투스라면 퍼미션을 주셧는지요..