当我重启设备时,我收到以下错误(我的应用程序在启动时启动,因为它被定义为启动器)。这种情况最近才开始发生,原因有些奇怪!
java.lang.RuntimeException:无法启动活动ComponentInfo {com.domain.application / com.domain.application.MainActivity}:java.lang.NullPointerException
LogCat中的异常指向:
loginBtn.setOnClickListener(new View.OnClickListener() {
// code here to deal with button press
}
loginBtn 是在 onCreate 开头附近定义的:
// Set activity view
setContentView(R.layout.activity_main);
// Initialise button
Button loginBtn = (Button)findViewById(R.id.loginButton);
活动xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#000000">
<EditText
android:id="@+id/screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/screen"
android:inputType="text"
android:layout_above="@+id/pw"
android:layout_centerHorizontal="true">
<requestFocus />
</EditText>
<EditText
android:id="@+id/pw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword"
android:layout_alignParentRight="false"
android:layout_centerInParent="true" />
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_below="@+id/pw"
android:text="@string/login"
android:layout_centerHorizontal="true"
android:layout_alignParentStart="false" />
</RelativeLayout>
我在SO上搜索了几乎所有这个错误的实例,并且我已经仔细检查了所有内容,并且我确定我的代码是正确的。
就像我说的那样,这只是刚刚开始发生,只有在设备启动时才会发生。
如果我调试应用程序(因此重新安装并启动)就可以了,没有错误。
表现XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.application"
android:versionCode="16"
android:versionName="1.1.5" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize"
android:screenOrientation="sensorLandscape">
<!--<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".PlaylistActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="sensorLandscape" >
</activity>
<activity
android:name=".WebViewActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="sensorLandscape"
android:hardwareAccelerated="true" >
</activity>
<receiver
android:name=".BootCompletedReceiver"
android:enabled="true"
android:exported="true"
android:label="Boot completed">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<service
android:enabled="true"
android:name=".PlaylistService" />
</application>
</manifest>