简体   繁体   English

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

[英]Application with ViewPager crashes at start when I set onClickListener

I've got a trouble. 我有麻烦了 When I set onClickListener on a Button, the application simply doesn't start. 当我在Button上设置onClickListener时,该应用程序根本无法启动。 I think that the promblem in wrong using ViewPager, but I can't find an answer in the Internet. 我认为使用ViewPager出错了,但是我在Internet上找不到答案。 Please tell me what is wrong in simpliest way, because I am new in Java. 请以最简单的方式告诉我哪里出了问题,因为我是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 (if I added setContentView(R.layout.activity_main); in onCreate): 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)

with setContentView(); 与setContentView(); you should specify your layout id, like 您应该指定布局ID,例如

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

   }

Try with that, and post your log 尝试一下,然后发布您的日志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM