【问题标题】:Why is onCreate crashing Android App when I call onClick?当我调用 onClick 时,为什么 onCreate 会导致 Android App 崩溃?
【发布时间】:2013-01-17 01:18:47
【问题描述】:

当我按下按钮时,我正在尝试加载新视图。我有一个 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

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);
}

}

【问题讨论】:

  • 第 36 行的代码是什么,你在哪里得到了空指针 ref?
  • listButton.setOnClickListener(new OnClickListener() {
  • 另外,R.layout.splash 中有什么?我认为它应该完全是 ViewPager。

标签: java android xml android-viewpager oncreate


【解决方案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);
        }
    });
}

【讨论】:

  • 它就在那里。我已经试过很多次了。多次重命名。
  • 根据您的 XML 帖子,您在 Splash2 中有按钮,但在上面的代码中,您正在加载 Spash。那是你的问题。
  • 您能告诉我如何访问我的 viewPager 上的按钮并能够使用它吗?请。
  • 首先,您现在意识到需要为 splash2.xml 调用 findViewById。这意味着您必须加载 splash2.xml。我不完全确定您想要什么行为,但我认为在 onCreate 中您需要加载 splash2 并将其放入 ViewPager - 即 myPager。然后,在 listButtonListener 中,您需要从 myPager 中取出该页面并在其上调用 findViewById。或者,您可以将 splash2 保存在成员变量中并在 listButtonListener 中使用它。有意义吗?
  • splash2 只是 viewPager 的一个视图。 viewPager 允许我在 4 个视图之间滑动。我必须加载“splash.xml”来加载 viewPager。我不明白什么是成员变量。我认为这是一个基本问题。它试图加载不存在的东西。我想我需要回到基础。
【解决方案2】:

你得到一个NullPointerException,可能是因为listButtonnull。确保splashB 确实存在于splash 布局中。

如果这不是答案,请尝试在 listButtonListener() 上设置一个断点,以找出它抛出 NullPointerException 的原因。

【讨论】:

  • 是的,我已经检查了很多次,是否存在拼写错误等简单错误。如果我删除 listButtonListener 它不会崩溃。如何使用 onClick 制作按钮?
  • 那个按钮在splash2,但是你正在使用splashsetContentView(R.layout.splash);
【解决方案3】:

你应该尝试删除

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;

}

【讨论】:

  • 这不是答案。没有理由这会导致崩溃并说“试试这个......”没有真正解释这可能是异常的原因不是答案。
  • 解释真的很简单,他无法获取Button ListButton视图,因为它还没有被Android应用程序创建。调用 setCurrentItem 会尝试加载不存在的数据。
  • 请告诉我如何解决这个问题。我知道这很简单。
  • 如果您使用的是 Eclipse,请在该行添加断点。我认为 findViewById() 有问题。您应该从 ViewPager 的 ViewGroup 中获取 View,然后在其上调用 findViewById() 以获取该按钮。
  • 更好的解决方案是将您的 onClick 侦听器添加到您创建的适配器中,因为您在其中获得焦点视图。
猜你喜欢
  • 2021-04-14
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多