[英]Error when attempting to extend and use Activity on Android
I am attempting to utilize an Activity
to enable Bluetooth on an Android device. 我正在尝试利用Activity
在Android设备上启用蓝牙。 The following is the class I have extended the Activity
class with. 以下是我扩展了Activity
类的类。 I am receiving a NullPointerException
error when the startActivityForResult()
method is called. 调用startActivityForResult()
方法时,我收到NullPointerException
错误。 Any suggestions? 有什么建议么? Thank you! 谢谢!
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);
}
}
The log output is as follows: 日志输出如下:
?:??: 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 file below - 以下清单文件-
<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>
Based on the comments in the OP, you are not starting the Activity
properly. 根据OP中的注释,您未正确启动Activity
。 You should never ever ever start an Activity
using new
. 您永远不要使用new
启动Activity
。
Firstly, your Activites
should look like this, calling connectBluetooth()
on its own. 首先,你的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);
}
}
When an Android app starts, it runs the main Activity
, which is defined in the manifest . 当Android应用启动时,它将运行manifest中定义的主Activity
。 if Activities
is your main Activity
, then no further code will be required. 如果“ Activities
是您的主要“ Activity
,则不需要其他代码。
However, I get the hunch that this is a second Activity
, which is started from somewhere else. 但是,我预感这是第二个Activity
,它是从其他地方开始的。 So let's say you have this: 因此,假设您有以下内容:
public class MainActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Activities testActivities = new Activities(); // This is bad!
testActivities.connectBluetooth(); // This too!
}
}
What you should be doing instead, is this: 您应该做的是:
public class MainActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, Activities.class)); // Starts an Activity properly
}
}
This will start your Activity
, call onCreate
, and create your Bluetooth connection properly. 这将启动您的Activity
,调用onCreate
,并正确创建您的蓝牙连接。 You should read this documentation on the Activity
lifecycle. 您应该在“ Activity
生命周期中阅读此文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.