我收到以下错误:
07-11 17:33:27.250:E / AndroidRuntime(1675):java.lang.RuntimeException:无法启动活动ComponentInfo {uk.co.myapp.gtvremote / uk.co.myapp.gtvremote.SlidepuzzleActivity}:显示java.lang.NullPointerException
不确定我做错了什么,欢迎任何建议和/或评论。
您也可以从代码中注意到我正在尝试重用AnymoteClientService。我假设我在这里做的方式是正确的,是吗? :
清单摘录:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.co.myapp.gtvremote"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name="com.example.google.tv.anymotelibrary.client.AnymoteClientService" >
</service>
<activity
android:name="com.example.google.tv.anymotelibrary.connection.PairingActivity"
android:configChanges="orientation"
android:label="Pairing with TV"
android:launchMode="singleTop" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SlidepuzzleActivity"
android:label="@string/title_activity_slidepuzzle" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="true" />
<uses-feature
android:name="android.hardware.wifi"
android:required="true" />
</manifest>
MainActivity.java摘录:
Button testButton = (Button) findViewById(R.id.testButton);
...
testButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, SlidepuzzleActivity.class);
//THIS IS WHERE I THINK THE EXCEPTION IS BEING THROWN
MainActivity.this.startActivity(myIntent);
}
});
SlidepuzzleActivity.java:
public class SlidepuzzleActivity extends Activity implements ClientListener{
private AnymoteClientService mAnymoteClientService;
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slidepuzzle);
ImageButton piece1x1 = (ImageButton) findViewById(R.id.piece1x1);
ImageButton piece1x2 = (ImageButton) findViewById(R.id.piece1x2);
ImageButton piece1x3 = (ImageButton) findViewById(R.id.piece1x3);
ImageButton piece2x1 = (ImageButton) findViewById(R.id.piece2x1);
ImageButton piece2x2 = (ImageButton) findViewById(R.id.piece2x2);
ImageButton piece2x3 = (ImageButton) findViewById(R.id.piece2x3);
ImageButton piece3x1 = (ImageButton) findViewById(R.id.piece3x1);
ImageButton piece3x2 = (ImageButton) findViewById(R.id.piece3x2);
ImageButton piece3x3 = (ImageButton) findViewById(R.id.piece3x3);
Intent intent = new Intent(mContext, AnymoteClientService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mConnection = new ServiceConnection() {
/*
* ServiceConnection listener methods.
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mAnymoteClientService = ((AnymoteClientService.AnymoteClientServiceBinder) service)
.getService();
mAnymoteClientService.attachClientListener(SlidepuzzleActivity.this);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mAnymoteClientService.detachClientListener(SlidepuzzleActivity.this);
mAnymoteClientService = null;
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.slidepuzzle, menu);
return true;
}
@Override
public void onConnected(AnymoteSender anymoteSender) {
// TODO Auto-generated method stub
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
@Override
public void onConnectionError() {
// TODO Auto-generated method stub
}
@Override
protected void onDestroy() {
if (mAnymoteClientService != null) {
mAnymoteClientService.detachClientListener(this);
}
unbindService(mConnection);
super.onDestroy();
}
}
干杯
请求堆栈跟踪:
07-11 17:33:27.250: E/AndroidRuntime(1675): FATAL EXCEPTION: main
07-11 17:33:27.250: E/AndroidRuntime(1675): java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.myapp.gtvremote/uk.co.myapp.gtvremote.SlidepuzzleActivity}: java.lang.NullPointerException
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.os.Looper.loop(Looper.java:137)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-11 17:33:27.250: E/AndroidRuntime(1675): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 17:33:27.250: E/AndroidRuntime(1675): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 17:33:27.250: E/AndroidRuntime(1675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-11 17:33:27.250: E/AndroidRuntime(1675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-11 17:33:27.250: E/AndroidRuntime(1675): at dalvik.system.NativeStart.main(Native Method)
07-11 17:33:27.250: E/AndroidRuntime(1675): Caused by: java.lang.NullPointerException
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.content.ComponentName.<init>(ComponentName.java:75)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.content.Intent.<init>(Intent.java:3175)
07-11 17:33:27.250: E/AndroidRuntime(1675): at uk.co.myapp.gtvremote.SlidepuzzleActivity.onCreate(SlidepuzzleActivity.java:39)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.app.Activity.performCreate(Activity.java:4465)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-11 17:33:27.250: E/AndroidRuntime(1675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-11 17:33:27.250: E/AndroidRuntime(1675): ... 11 more
我已经在SlidepuzzleActivity.java摘录中标记了抛出异常//这就是我认为异常正在发生的地方
答案 0 :(得分:2)
据我所知,mContext为null;并且在实例化Intent时可能会导致此异常。