当我按下按钮时,我正在尝试加载新视图。我有一个viewPager,按钮在这些视图上。当我打电话给onClick我的应用程序永远不会启动,它会在任何事情发生之前崩溃。我一直试图将这个bug压扁一个多星期,我认为这是非常业余的。请帮助学生程序员!
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
splash.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>
splash2.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>
ViewPager:
@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);
}
}
答案 0 :(得分:1)
根据你在listButton.setOnClickListener上的失败,我猜你的布局中没有找到R.id.splashB。
public class Main extends Activity {
Button listButton;
View splash2; // Added
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setContentView(R.layout.splash);
SplashPager adapter = new SplashPager();
ViewPager myPager = (ViewPager) findViewById(R.id.splashPager);
....
LayoutInflater inflater = getLayoutInflater(); // Added
splash2 = inflater.inflate (R.layout.splash2, null); // Added
myPager.add (splash2); // Added
listButtonListener();
}
public void listButtonListener() {
listButton = (Button) splash2.findViewById(R.id.splashB); // Changed
listButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setContentView(R.layout.list_layout);
}
});
}
答案 1 :(得分:0)
您获得NullPointerException
,可能是因为listButton
是null
。确保splashB
布局中确实存在splash
。
如果这不是答案,请尝试在listButtonListener()
上设置一个断点,以找出它引发NullPointerException
的原因。
答案 2 :(得分:-1)
你应该尝试删除
myPager.setCurrentItem(0);
此处不需要此,因为默认情况下ViewPager的默认页面为0。 这样您就可以尝试显示Android系统可能无法创建的页面。
编辑:
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(this); // PASSING MAIN ACTIVITY FOR SETTING CONTENTVIEW
ViewPager myPager = (ViewPager) findViewById(R.id.splashPager);
myPager.setAdapter(adapter);
}...}
//您的适配器代码
@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);
if(resId == R.layout.splash2) {
Button b = (Button) view.findViewById(R.id.splashB);
b.setOnClickListner(new OnClickListener() {
@Override
public void onClick(View arg0) {
// mainActivity is Activity variable passed in Adapter constructor
mainActivity.setContentView(R.layout.list_layout);
}
});
}
((ViewPager) collection).addView(view, 0);
return view;
}