我遇到的问题是我无法开始活动。
以下是开始新活动之前上一个活动的代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
registerClickCallback();
}
private void registerClickCallback() {
ListView list = (ListView)findViewById(R.id.listViewTop100);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent;
switch(position)
{
case 0:
intent = new Intent(MainActivity.this,NextactivityActivity.class);
startActivity(intent);
break;
case 1:
intent = new Intent(MainActivity.this,NextTvSeriesActivity.class);
startActivity(intent);
break;
case 2:
intent = new Intent(MainActivity.this,Next100moviesActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
}
(我想要开始的活动是案例1 / NextTvSeriesActivity) 这是我想要开始的活动的代码:
package com.example.its_time.mainmenu;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.its_time.R;
public class NextTvSeriesActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_tv_series);
pupulateListView();
registerClickCallback();
}
private void registerClickCallback() {
ListView list = (ListView)findViewById(R.id.listViewTop100);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent;
switch(position)
{
case 0:
intent = new Intent(NextTvSeriesActivity.this,TvSeriesActiongenreActivity.class);
startActivity(intent);
break;
case 1:
intent = new Intent(NextTvSeriesActivity.this,TvSeriesAnimationgenreActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
}
这是android清单代码:
<activity
android:name="com.example.its_time.mainmenu.NextTvSeriesActivity"
android:label="@string/title_activity_next_tv_series" >
</activity>
com.example.its_time.mainmenu是包namne。
我检查过清单文件,拼写是否正确,活动是在同一个文件夹中,可能是什么错误?
以下是整个错误:
12-20 19:43:53.037: E/AndroidRuntime(2071): FATAL EXCEPTION: main
12-20 19:43:53.037: E/AndroidRuntime(2071): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.its_time/com.example.its_time.mainmenu.NextTvSeriesActivity}: java.lang.NullPointerException
12-20 19:43:53.037: E/AndroidRuntime(2071): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-20 19:59:15.371: E/AndroidRuntime(3182): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-20 19:59:15.371: E/AndroidRuntime(3182): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-20 19:59:15.371: E/AndroidRuntime(3182): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-20 19:59:15.371: E/AndroidRuntime(3182): at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 19:59:15.371: E/AndroidRuntime(3182): at android.os.Looper.loop(Looper.java:137)
12-20 19:59:15.371: E/AndroidRuntime(3182): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-20 19:59:15.371: E/AndroidRuntime(3182): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 19:59:15.371: E/AndroidRuntime(3182): at java.lang.reflect.Method.invoke(Method.java:511)
12-20 19:59:15.371: E/AndroidRuntime(3182): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-20 19:59:15.371: E/AndroidRuntime(3182): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-20 19:59:15.371: E/AndroidRuntime(3182): at dalvik.system.NativeStart.main(Native Method)
12-20 19:59:15.371: E/AndroidRuntime(3182): Caused by: java.lang.NullPointerException
12-20 19:59:15.371: E/AndroidRuntime(3182): at com.example.its_time.mainmenu.NextTvSeriesActivity.registerClickCallback(NextTvSeriesActivity.java:29)
12-20 19:59:15.371: E/AndroidRuntime(3182): at com.example.its_time.mainmenu.NextTvSeriesActivity.onCreate(NextTvSeriesActivity.java:21)
12-20 19:59:15.371: E/AndroidRuntime(3182): at android.app.Activity.performCreate(Activity.java:4465)
12-20 19:59:15.371: E/AndroidRuntime(3182): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-20 19:59:15.371: E/AndroidRuntime(3182): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-20 19:59:15.371: E/AndroidRuntime(3182): ... 11 more
答案 0 :(得分:2)
在next_tv_series
中调用时,您的listViewTop100
布局没有ID findViewById()
的视图,NextTvSeriesActivity.registerClickCallback()
返回null。在null上调用方法(setOnItemClickListener()
)会导致异常。