[英]I can't find the error causing a null pointer exception
I am new to android programming and I am trying to create a simple home screen replacement/launcher using a viewpager. 我是android编程的新手,我正在尝试使用viewpager创建一个简单的主屏幕替换/启动器。
When I try to run the application, it starts up and then immediately force closes. 当我尝试运行该应用程序时,它将启动,然后立即关闭。 I think it is something to do with a null pointer exception but I am really new to programming and can't find the source of the error. 我认为这与空指针异常有关,但是我真的是编程新手,无法找到错误的来源。
This is my logcat. 这是我的日志。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dntmdp.matthewhuckel.simplephonelauncher/com.dntmdp.matthewhuckel.simplephonelauncher.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
// etc...
Caused by: java.lang.NullPointerException
at com.dntmdp.matthewhuckel.simplephonelauncher.MainActivity.onCreate(MainActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
// etc...
08-13 20:10:58.585 1805-1805/com.dntmdp.matthewhuckel.simplephonelauncher I/Process﹕ Sending signal. PID: 1805 SIG: 9
This is my main activity java class. 这是我的主要活动java类。
public class MainActivity extends ActionBarActivity {
Button phone;
// more buttons
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton phone = (ImageButton) findViewById(R.id.imageButton);
phone.setOnClickListener(new View.OnClickListener() { // Exception thrown on this line
@Override
public void onClick(View view) {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.phone");
startActivity(LaunchIntent);
}
});
//setting more onclicklisteners in exactly the same manner
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager_layout);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
// menu initialization
}
I found the answer. 我找到了答案。 I was trying to find the image button id's in the activity_main xml, but they were in other activities. 我试图在activity_main xml中找到图像按钮ID,但它们在其他活动中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.