简体   繁体   English

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

[英]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.

相关问题 Android Studio 错误:活动必须扩展 android.app.activity - Android Studio Error: Activity must extend android.app.activity 尝试在Java中使用自定义比较器时出错 - Error when attempting to use custom comparator in Java 我在Android Studio中扩展AppCompatActivity时出错 - Error when i extend AppCompatActivity in Android studio 尝试在Java中使用List &lt;&gt;时出现“非通用”错误 - Getting “not generic” error when attempting to use List<> In Java Android Azure:404尝试从服务器检索数据时出错 - Android Azure: 404 Error when attempting to retrieve data from the Server 尝试使用SMTP Appender时出现Log4J2工厂错误 - Log4J2 Factory Error when attempting to use SMTP Appender 尝试使用可变长度参数时出现不兼容的类型错误 - Incompatible type error when attempting to use variable length argument 尝试使用生成的命名查询时,sqlException错误 - Error in sqlException when attempting to use generated named query Java错误尝试将方法的返回值用作if语句中的项时 - Java Error When attempting to use the return from a method as an item in an if statement “找不到源”错误-尝试实现简单的Activity-Fragment Android应用程序 - 'Source Not Found' error - attempting to implement simple Activity-Fragment Android application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM