Java 我能';找不到导致空指针异常的错误
我是android编程新手,我正在尝试使用viewpager创建一个简单的主屏幕替换/启动程序 当我尝试运行应用程序时,它会启动,然后立即强制关闭。我认为这与空指针异常有关,但我对编程非常陌生,无法找到错误的来源 这是我的日志Java 我能';找不到导致空指针异常的错误,java,android,nullpointerexception,android-launcher,Java,Android,Nullpointerexception,Android Launcher,我是android编程新手,我正在尝试使用viewpager创建一个简单的主屏幕替换/启动程序 当我尝试运行应用程序时,它会启动,然后立即强制关闭。我认为这与空指针异常有关,但我对编程非常陌生,无法找到错误的来源 这是我的日志 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dntmdp.matthewhuckel.simplephonelauncher/com.dntmdp.matthewhu
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
}
我找到了答案。我试图在活动\u主xml中查找图像按钮id,但它们在其他活动中。活动中的第47行是哪一行?使用
setContentView()
设置的布局集没有您尝试的视图findViewById()
。在main activity.java
中哪一行是47?phone.setOnClickListenser是第47行。如果代码是代码的精确副本,则第47行是phone.setOnClickListener
:activity\u main.xml
不包含R.id.imageButton
的定义