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

时间:2014-08-14 05:51:46

标签: java android nullpointerexception android-launcher

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

当我尝试运行应用程序时,它会启动,然后立即强制关闭。我认为它与空指针异常有关,但我对编程很新,无法找到错误的来源。

这是我的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

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

1 个答案:

答案 0 :(得分:0)

我找到了答案。我试图在activity_main xml中找到图片按钮ID,但他们正在进行其他活动。