デバイスの Bluetooth を使用する Android アプリケーションを作成しようとしています。ただし、アプリケーションをエミュレートしようとすると、次のエラーが表示されます: 残念ながらアプリケーションが停止しました。
解決策を探しましたが、問題はありましたが、次のことを行いました。
- Bluetooth 機能はエミュレートできないため、エミュレータではなく実際のデバイスを使用してください。
- オンラインで見つけたいくつかの提案に従ってコードを変更します
- Bluetooth 対応のコンピューターで、通常のエミュレーターの代わりに VM を使用します。
上記の試みはすべて無駄であることが判明し、役に立ちませんでした。
私のJavaファイル:
package com.example.application;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity{
/** Called when the activity is first created. */
private BluetoothAdapter btAdapter;
public TextView statusUpdate;
public Button connect;
public Button disconnect;
public ImageView logo;
BroadcastReceiver bluetoothState = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
String prevStateExtra=BluetoothAdapter.EXTRA_PREVIOUS_STATE;
String stateExtra=BluetoothAdapter.EXTRA_STATE;
int state = intent.getIntExtra(prevStateExtra, -1);
int previousState = intent.getIntExtra(prevStateExtra, -1);
String toastText="";
switch(state){
case(BluetoothAdapter.STATE_TURNING_ON) :
{
toastText="Bluetooth turning on";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
break;
}
case(BluetoothAdapter.STATE_ON) :
{
toastText="Bluetooth on";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
setupUI();
break;
}
case(BluetoothAdapter.STATE_TURNING_OFF) :
{
toastText="Bluetooth turning off";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
break;
}
case(BluetoothAdapter.STATE_OFF) :
{
toastText="Bluetooth off";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
setupUI();
break;
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
}//end onCreate
private void setupUI(){
//get references
//final TextView statusUpdate = (TextView) findViewById(R.id.result);
final Button connect = (Button)findViewById(R.id.connect);
final Button disconnect = (Button)findViewById(R.id.disconnect);
//final ImageView logo = (ImageView)findviewById(R.id.logo);
//set display view
disconnect.setVisibility(View.GONE);
logo.setVisibility(View.GONE);
btAdapter = BluetoothAdapter.getDefaultAdapter();
if(btAdapter.isEnabled()){
String address = btAdapter.getAddress();
String name = btAdapter.getName();
String statusText = name + " : " + address;
statusUpdate.setText(statusText);
}
else{
connect.setVisibility(View.VISIBLE);
statusUpdate.setText("Bluetooth is not on");
}
connect.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
String actionStateChanged = BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED;
String actionRequestEnable = BluetoothAdapter.ACTION_REQUEST_ENABLE;
IntentFilter filter = new IntentFilter(actionStateChanged);
registerReceiver(bluetoothState, filter);
startActivityForResult(new Intent(actionRequestEnable), 0);
}
}); //end connect onClickListener
disconnect.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
}
}); //end disconnect onClickListener
}
} //end setupUI
Main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="Sherline Android App"
android:textColor="#0000ff"
android:textSize="20dp"
android:textStyle="bold" />
<requestFocus />
<Button
android:id="@+id/connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="36dp"
android:text="Connect Bluetooth"
android:textSize="12sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText1"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_alignTop="@+id/connect"
android:layout_toRightOf="@+id/disconnect"
android:background="#808080"
android:ems="10"
android:hint="G-code goes here..."
android:inputType="textMultiLine"
android:textColorHint="#FFFFFFFF"
android:width="150dp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/disconnect"
android:layout_alignRight="@+id/disconnect"
android:layout_below="@+id/disconnect"
android:radius="3dp"
android:text="Stop"
android:textSize="12dp"
android:textStyle="bold" />
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button01"
android:layout_alignRight="@+id/Button01"
android:layout_centerVertical="true"
android:radius="3dp"
android:text="Pause"
android:textSize="12dp"
android:textStyle="bold" />
<Button
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button02"
android:layout_alignRight="@+id/Button02"
android:layout_below="@+id/Button02"
android:radius="3dp"
android:text="Resume"
android:textSize="12dp"
android:textStyle="bold" />
<Button
android:id="@+id/disconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/connect"
android:layout_below="@+id/connect"
android:radius="3dp"
android:text="Disconnect Bluetooth"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
logcat は次のエラーを示します。
01-31 10:34:41.511: E/AndroidRuntime(978): FATAL EXCEPTION: main
01-31 10:34:41.511: E/AndroidRuntime(978): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.MainActivity}: java.lang.NullPointerException
01-31 10:34:41.511: E/AndroidRuntime(978): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-31 10:34:41.511: E/AndroidRuntime(978): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-31 10:34:41.511: E/AndroidRuntime(978): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-31 10:34:41.511: E/AndroidRuntime(978): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-31 10:34:41.511: E/AndroidRuntime(978): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 10:34:41.511: E/AndroidRuntime(978): at android.os.Looper.loop(Looper.java:137)
01-31 10:34:41.511: E/AndroidRuntime(978): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-31 10:34:41.511: E/AndroidRuntime(978): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 10:34:41.511: E/AndroidRuntime(978): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 10:34:41.511: E/AndroidRuntime(978): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-31 10:34:41.511: E/AndroidRuntime(978): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-31 10:34:41.511: E/AndroidRuntime(978): at dalvik.system.NativeStart.main(Native Method)
01-31 10:34:41.511: E/AndroidRuntime(978): Caused by: java.lang.NullPointerException
01-31 10:34:41.511: E/AndroidRuntime(978): at com.example.application.MainActivity.setupUI(MainActivity.java:87)
01-31 10:34:41.511: E/AndroidRuntime(978): at com.example.application.MainActivity.onCreate(MainActivity.java:73)
01-31 10:34:41.511: E/AndroidRuntime(978): at android.app.Activity.performCreate(Activity.java:5104)
01-31 10:34:41.511: E/AndroidRuntime(978): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-31 10:34:41.511: E/AndroidRuntime(978): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-31 10:34:41.511: E/AndroidRuntime(978): ... 11 more
01-31 10:54:41.175: E/AndroidRuntime(1149): FATAL EXCEPTION: main
01-31 10:54:41.175: E/AndroidRuntime(1149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.MainActivity}: java.lang.NullPointerException
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.os.Looper.loop(Looper.java:137)
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-31 10:54:41.175: E/AndroidRuntime(1149): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 10:54:41.175: E/AndroidRuntime(1149): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 10:54:41.175: E/AndroidRuntime(1149): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-31 10:54:41.175: E/AndroidRuntime(1149): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-31 10:54:41.175: E/AndroidRuntime(1149): at dalvik.system.NativeStart.main(Native Method)
01-31 10:54:41.175: E/AndroidRuntime(1149): Caused by: java.lang.NullPointerException
01-31 10:54:41.175: E/AndroidRuntime(1149): at com.example.application.MainActivity.setupUI(MainActivity.java:87)
01-31 10:54:41.175: E/AndroidRuntime(1149): at com.example.application.MainActivity.onCreate(MainActivity.java:73)
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.app.Activity.performCreate(Activity.java:5104)
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-31 10:54:41.175: E/AndroidRuntime(1149): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-31 10:54:41.175: E/AndroidRuntime(1149): ... 11 more
助けてください!ありがとう!