OnResume方法抛出NullPointerException

时间:2015-07-26 11:25:24

标签: android android-studio

我正在尝试在操作栏中添加项目,但是当我点击MainActivity中的项目时,它会显示NullPointer异常。 它应该开始这项活动。我已经在我的Android清单文件中注册了该活动! 这是我的代码。

package milind.com.bluetooth_transmission;

public class settings extends ActionBarActivity {
    TextView textConnectionStatus;
    ListView pairedListView;
    private BluetoothAdapter mBtAdapter;
    private ArrayAdapter<String> mPairedDevicesArrayAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
    {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.settings);
        // Member fields
        Intent intent = getIntent();
        textConnectionStatus = (TextView) findViewById(R.id.connecting);
        textConnectionStatus.setTextSize(40);

        // Initialize array adapter for paired devices
        mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

        // Find and set up the ListView for paired devices
        pairedListView = (ListView) findViewById(R.id.paired_devices);
        pairedListView.setAdapter(mPairedDevicesArrayAdapter);


    }
    @Override
    public void onResume()
    {
        super.onResume();
        //It is best to check BT status at onResume in case something has changed while app was paused etc
        checkBTState();

        mPairedDevicesArrayAdapter.clear();// clears the array so items aren't duplicated when resuming from onPause

        textConnectionStatus.setText(" "); //makes the textview blank

        // Get the local Bluetooth adapter
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();

        // Get a set of currently paired devices and append to pairedDevices list
        Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

        // Add previously paired devices to the array
        if (pairedDevices.size() > 0) {
            findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);//make title viewable
            for (BluetoothDevice device : pairedDevices) {
                mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        } else {
            mPairedDevicesArrayAdapter.add("no devices paired");
        }
    }

    //method to check if the device has Bluetooth and if it is on.
    //Prompts the user to turn it on if it is off
    public void checkBTState()
    {
        // Check device has Bluetooth and that it is turned on
        mBtAdapter=BluetoothAdapter.getDefaultAdapter(); // CHECK THIS OUT THAT IT WORKS!!!
        if(mBtAdapter==null) {
            Toast.makeText(getBaseContext(), "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            if (!mBtAdapter.isEnabled()) {
                //Prompt user to turn on Bluetooth
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, 1);
            }
        }
    }
}

这是我的堆栈跟踪:

07-26 16:42:58.492  18894-18894/milind.com.bluetooth_transmission E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: milind.com.bluetooth_transmission, PID: 18894
java.lang.RuntimeException: Unable to resume activity {milind.com.bluetooth_transmission/milind.com.bluetooth_transmission.settings}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.clear()' on a null object reference
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2980)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3011)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394)
    at android.app.ActivityThread.access$900(ActivityThread.java:147)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.clear()' on a null object reference
    at milind.com.bluetooth_transmission.settings.onResume(settings.java:53)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
    at android.app.Activity.performResume(Activity.java:6019)
    at     android.app.ActivityThread.performResumeActivity(ActivityThread.java:2969)

2 个答案:

答案 0 :(得分:1)

您覆盖了错误的onCreate方法。你需要覆盖:

protected void onCreate (Bundle savedInstanceState)

答案 1 :(得分:0)

当调用onResume时,所有类成员都会重新初始化为其默认值,在本例中为null。

http://developer.android.com/training/basics/activity-lifecycle/pausing.html

  

当用户从Paused状态恢复活动时,系统会调用onResume()方法。   请注意,每当您的活动进入前台时系统都会调用此方法,包括第一次创建活动时。因此,您应该实现onResume()来初始化您在onPause()期间释放的组件,并执行每次活动进入Resumed状态时必须发生的任何其他初始化(例如开始动画和初始化仅在活动具有用户时使用的组件)焦点)。