当我按下按钮时,我正在尝试加载新视图。我有一个 viewPager,按钮在这些视图上。当我调用 onClick 时,我的应用程序永远不会启动,它会在任何事情发生之前崩溃。一个多星期以来,我一直在努力解决这个错误,我认为这是非常业余的事情。请帮助学生程序员!
public class Main extends Activity {
Button listButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
SplashPager adapter = new SplashPager();
ViewPager myPager = (ViewPager) findViewById(R.id.splashPager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
listButtonListener();
}
public void listButtonListener() {
listButton = (Button) findViewById(R.id.splashB);
listButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setContentView(R.layout.list_layout);
}
});
}
}
日志:
01-16 20:09:49.149: W/dalvikvm(29035): threadid=1: thread exiting with uncaught exception (group=0x40207560)
01-16 20:09:49.149: E/AndroidRuntime(29035): FATAL EXCEPTION: main
01-16 20:09:49.149: E/AndroidRuntime(29035): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.survtest2/com.example.survtest2.Main}: java.lang.NullPointerException
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.os.Looper.loop(Looper.java:130)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.main(ActivityThread.java:3733)
01-16 20:09:49.149: E/AndroidRuntime(29035): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 20:09:49.149: E/AndroidRuntime(29035): at java.lang.reflect.Method.invoke(Method.java:507)
01-16 20:09:49.149: E/AndroidRuntime(29035): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-16 20:09:49.149: E/AndroidRuntime(29035): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-16 20:09:49.149: E/AndroidRuntime(29035): at dalvik.system.NativeStart.main(Native Method)
01-16 20:09:49.149: E/AndroidRuntime(29035): Caused by: java.lang.NullPointerException
01-16 20:09:49.149: E/AndroidRuntime(29035): at com.example.survtest2.Main.listButtonListener(Main.java:36)
01-16 20:09:49.149: E/AndroidRuntime(29035): at com.example.survtest2.Main.onCreate(Main.java:30)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-16 20:09:49.149: E/AndroidRuntime(29035): ... 11 more
01-16 20:09:49.149: E/AndroidRuntime(29035): [Blue Error Handler] Make Debugging Report file for main
01-16 20:09:49.149: E/AndroidRuntime(29035): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.survtest2/com.example.survtest2.Main}: java.lang.NullPointerException
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.os.Looper.loop(Looper.java:130)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.main(ActivityThread.java:3733)
01-16 20:09:49.149: E/AndroidRuntime(29035): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 20:09:49.149: E/AndroidRuntime(29035): at java.lang.reflect.Method.invoke(Method.java:507)
01-16 20:09:49.149: E/AndroidRuntime(29035): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-16 20:09:49.149: E/AndroidRuntime(29035): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-16 20:09:49.149: E/AndroidRuntime(29035): at dalvik.system.NativeStart.main(Native Method)
01-16 20:09:49.149: E/AndroidRuntime(29035): Caused by: java.lang.NullPointerException
01-16 20:09:49.149: E/AndroidRuntime(29035): at com.example.survtest2.Main.listButtonListener(Main.java:36)
01-16 20:09:49.149: E/AndroidRuntime(29035): at com.example.survtest2.Main.onCreate(Main.java:30)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-16 20:09:49.149: E/AndroidRuntime(29035): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-16 20:09:49.149: E/AndroidRuntime(29035): ... 11 more
飞溅.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/splashPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
飞溅2.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/intro__1" />
<Button
android:id="@+id/splashB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="75dp"
android:text="aaaaaaaaaaaaaaaaaa" />
</FrameLayout>
浏览器:
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.splash2;
break;
case 1:
resId = R.layout.splash3;
break;
case 2:
resId = R.layout.splash4;
break;
case 3:
resId = R.layout.splash5;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == ((View) arg1);
}
}