尝试在Android上扩展和使用Activity时出错

时间:2012-09-09 19:21:09

标签: java android android-intent android-activity

我正在尝试利用Activity在Android设备上启用蓝牙功能。以下是我扩展Activity类的类。调用NullPointerException方法时,我收到startActivityForResult()错误。有什么建议?谢谢!

public class Activities extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    protected void connectBluetooth(){
        Intent intentBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

        startActivityForResult(intentBluetooth, 0); 
    }
}

日志输出如下:

?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?): java.lang.NullPointerException
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at android.app.Activity.startActivityForResult(Activity.java:3351)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at android.app.Activity.startActivityForResult(Activity.java:3312)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at com.eyesore.bluetooth.Activities.connectBluetooth(Activities.java:28)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at com.eyesore.bluetooth.BluetoothModule.example(BluetoothModule.java:77)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.runtime.v8.V8Object.nativeFireEvent(Native Method)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.runtime.v8.V8Object.fireEvent(V8Object.java:60)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.KrollProxy.doFireEvent(KrollProxy.java:636)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:831)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:307)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at android.os.Handler.dispatchMessage(Handler.java:95)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at android.os.Looper.loop(Looper.java:137)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15246 W/System.err ]
?:??: W/?(?):   at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:104)
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15231 E/TiJSError ]
?:??: W/?(?): (main) [6,293329] ----- Titanium Javascript Runtime Error -----
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15231 E/TiJSError ]
?:??: W/?(?): (main) [0,293329] - In app.js:71,12
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15231 E/TiJSError ]
?:??: W/?(?): (main) [0,293329] - Message: Uncaught Error: Java Exception occurred
?:??: W/?(?): [ 09-09 18:08:38.497 15231:15231 E/TiJSError ]
?:??: W/?(?): (main) [0,293329] - Source:   bluetooth.example();
?:??: W/?(?): [ 09-09 18:08:38.517 15231:15246 E/V8Exception ]
?:??: W/?(?): Exception occurred at app.js:71: Uncaught Error: Java Exception occurred

下面的清单文件 -

<manifest>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.bluetooth.adapter.action.REQUEST_ENABLE"/>
        <application>
            <activity android:name=".Activities" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>            
</manifest>

1 个答案:

答案 0 :(得分:0)

根据OP中的评论,您没有正确启动Activity。您应该永远不会使用Activity启动new

首先,您的Activites应如下所示,并自行调用connectBluetooth()

public class Activities extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        connectBluetooth();
    }
    protected void connectBluetooth(){
        Intent intentBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

        startActivityForResult(intentBluetooth, 0); 
    }
}

当Android应用启动时,它会运行主Activity,即defined in the manifest如果Activities是您的主要Activity ,则不需要进一步的代码。

然而,我预感到这是第二个Activity,它是从其他地方开始的。所以,假设你有这个:

public class MainActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activities testActivities = new Activities(); // This is bad!
        testActivities.connectBluetooth(); // This too!
    }
}

你应该做的事情是:

public class MainActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startActivity(new Intent(this, Activities.class)); // Starts an Activity properly
    }
}

这将启动您的Activity,致电onCreate,并正确创建蓝牙连接。您应该在Activity生命周期内阅读this documentation