繁体   English   中英

我找不到导致空指针异常的错误

[英]I can't find the error causing a null pointer exception

我是android编程的新手,我正在尝试使用viewpager创建一个简单的主屏幕替换/启动器。

当我尝试运行该应用程序时,它将启动,然后立即关闭。 我认为这与空指针异常有关,但是我真的是编程新手,无法找到错误的来源。

这是我的日志。

    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

这是我的主要活动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
}

我找到了答案。 我试图在activity_main xml中找到图像按钮ID,但它们在其他活动中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM