java - 当我设置 onClickListener 时,带有 ViewPager 的应用程序在启动时崩溃

标签 java android

我遇到了麻烦。当我在按钮上设置 onClickListener 时,应用程序根本无法启动。我认为问题出在错误使用ViewPager,但我在互联网上找不到答案。请用最简单的方式告诉我哪里出了问题,因为我是 Java 新手。

  public class MainActivity extends ActionBarActivity {
    CheckBox percentageBox;
    CheckBox numberBox;
        EditText editText;
        TextView textView;
        Button saveButton;
        Button generateButton;
        View.OnClickListener saveButtonListener;
        View.OnClickListener generateButtonListener;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LayoutInflater inflater = LayoutInflater.from(this);
            List<View> pages = new ArrayList<View>();
            View page1 = inflater.inflate(R.layout.passwordmanager, null);
            pages.add(page1);
            View page2 = inflater.inflate(R.layout.passwordgenerator, null);
            pages.add(page2);

            SamplePagerAdapter pagerAdapter = new SamplePagerAdapter(pages);
            ViewPager viewPager = new ViewPager(this);
            viewPager.setAdapter(pagerAdapter);
            viewPager.setCurrentItem(1);
            setContentView(viewPager);
            initializeApp();
        }

    public void initializeApp() {

        percentageBox = (CheckBox) findViewById(R.id.checkBox2);
        numberBox = (CheckBox) findViewById(R.id.checkBox);
        textView = (TextView) findViewById(R.id.textView2);   
        generateButton=(Button) findViewById(R.id.button);       
        generateButtonListener= new View.OnClickListener(){
            public void onClick(View v){
                touchGenerateButton();
            }
        };
        generateButton.setOnClickListener(generateButtonListener);//Here is the problem

        private void touchGenerateButton() {
            textView.setText(getPasswordString(Integer.parseInt(editText.getText().toString()),numberBox.isChecked(),percentageBox.isChecked()));
        }

LogCat(如果我在 onCreate 中添加了 setContentView(R.layout.activity_main);):

02-21 06:21:26.121    1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-21 06:21:26.121    1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 11346: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-21 06:21:26.121    1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-21 06:21:26.121    1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-21 06:21:26.121    1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-21 06:21:26.121    1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-21 06:21:26.121    1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-21 06:21:26.121    1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 9040: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-21 06:21:26.121    1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-21 06:21:26.125    1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-21 06:21:26.125    1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-21 06:21:26.125    1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-21 06:21:26.129    1697-1697/com.example.rinat.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-21 06:21:26.129    1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
02-21 06:21:26.129    1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-21 06:21:26.141    1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 115K, 5% free 3702K/3888K, paused 4ms, total 4ms
02-21 06:21:26.153    1697-1697/com.example.rinat.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 25K, 6% free 3897K/4112K, paused 9ms, total 9ms
02-21 06:21:26.161    1697-1697/com.example.rinat.myapplication I/dalvikvm-heap﹕ Grow heap (frag case) to 6.291MB for 2536932-byte allocation
02-21 06:21:26.205    1697-1706/com.example.rinat.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 6375K/6592K, paused 52ms, total 52ms
02-21 06:21:26.457    1697-1697/com.example.rinat.myapplication D/AndroidRuntime﹕ Shutting down VM
02-21 06:21:26.457    1697-1697/com.example.rinat.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4ca3b20)
02-21 06:21:26.541    1697-1697/com.example.rinat.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.rinat.myapplication, PID: 1697
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rinat.myapplication/com.example.rinat.myapplication.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.rinat.myapplication.MainActivity.initializeApp(MainActivity.java:68)
            at com.example.rinat.myapplication.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:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

最佳答案

与 setContentView();你应该指定你的布局id,比如

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity); // or your layout id
            ....

   }

尝试一下,然后发布你的日志

关于java - 当我设置 onClickListener 时,带有 ViewPager 的应用程序在启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28630517/

相关文章:

java - 两个连接的 JPQL 并集

java - Android 上如何知道字符串是否在数组内?

java - 为什么使用 SimpleDateFormat 从字符串解析日期时得到错误的结果? ( java )

Android获取本地日期类型的简称

java - 只有一个线程在我的 JFrame 对象中绘制,为什么另一个线程不呢?

Java卡通信

android - 如何在 v2 谷歌地图中添加多个标记和叠加层?

android - forceLayout(), requestLayout()

android - 调用函数并将参数从 Kotlin 传递给 Flutter

android - 如何将我的 Android 应用程序添加到共享对话框?