1

我正在尝试创建一个使用设备蓝牙的 Android 应用程序。但是,当我尝试模拟应用程序时,它给了我这个错误:不幸的是,应用程序已停止。

我已经搜索了解决方案,但问题是,我已经完成了以下操作:

  1. 使用实际设备而不是模拟器,因为无法模拟蓝牙功能
  2. 根据我在网上找到的一些建议修改代码
  3. 在支持蓝牙的计算机上使用虚拟机而不是普通模拟器。

以上所有的尝试都被证明是徒劳的,没有任何效果。

我的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

主要.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

请帮忙!谢谢!

4

3 回答 3

4

好吧,你得到了NullPointerException,可能有 2 个感兴趣的案例。

  1. statusUpdate, 和logo变量是NULL, 但其他答案已经解释了这一点。

  2. BluetoothAdapter.getDefaultAdapter()NULL如果硬件不支持蓝牙,可能会返回。所以你也需要处理这种情况..

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    if(btAdapter != null && btAdapter.isEnabled()){  // see changes in this line..
    
于 2013-01-31T11:20:30.667 回答
3

问题是这一行有空指针:

       logo.setVisibility(View.GONE);

取消注释这一行:

         //final ImageView logo = (ImageView)findviewById(R.id.logo);

编辑:statusUpdate 有同样的问题。

于 2013-01-31T11:14:07.857 回答
1

一个问题...该组件未连接到您的布局:

statusUpdate.setText(statusText);

我认为你需要这样的东西:

statusUpdate = (TextView)findViewById(R.id.textView1);
于 2013-01-31T11:13:09.560 回答