当我尝试实现onClickListener时,应用程序崩溃了

时间:2018-04-24 19:13:12

标签: android onclicklistener

我用按钮制作ViewPager。 我试图获取按钮的id并将onclicklistener设置为viewpager类中的该按钮。

我有一个问题。 我尝试过像这样的东西。

Button btn1 = (Button) findViewById(R.id.gimnazijablbtn1);

btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch(v.getId()) {

                    case R.id.gimnazijablbtn1:
                        Intent Banjaluka = new Intent(banjaluka_activity.this,PocetniActivity.class);
                        startActivity(Banjaluka);
                        break;
                }

            }
        });`

但是我的应用程序崩溃了。

这是日志 04-24 20:46:42.574 24297-24297/com.electroniccode.ln.vodickrozzanimanja E/AndroidRuntime: FATAL EXCEPTION: main Process: com.electroniccode.ln.vodickrozzanimanja, PID: 24297 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.electroniccode.ln.vodickrozzanimanja/com.electroniccode.ln.vodickrozzanimanja.banjaluka_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) at android.app.ActivityThread.access$900(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5551) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117) at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149) at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29) at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54) at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190) at com.electroniccode.ln.vodickrozzanimanja.banjaluka_activity.<init>(banjaluka_activity.java:46) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)  at android.app.ActivityThread.access$900(ActivityThread.java:157)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5551)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

2 个答案:

答案 0 :(得分:0)

试试这个。您正在onCreate方法之外初始化按钮。应在setContentView(R.layout.activity_banjaluka_activity);

之后在onCreate中初始化视图
Button btn1;

/**
 * The {@link ViewPager} that will host the section contents.
 */
private ViewPager mViewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_banjaluka_activity);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));


    btn1 = (Button) findViewById(R.id.gimnazijablbtn1);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           Intent Banjaluka = new Intent(banjaluka_activity.this,PocetniActivity.class);
                    startActivity(Banjaluka);
                    break;
        }
    });
}

答案 1 :(得分:0)

3个简单的建议:

  1. 使用方法onPostCreate
  2. 获取和设置文字和按钮等视图
  3. 您正在正确设置OnClickListener,但您不需要过滤视图ID,因为只会为您设置的视图调用侦听器。
  4. 我建议让您的活动实现View.OnClickListener并将其作为侦听器传递给您想要收听的所有视图。然后,您可以为视图ID执行switch语句并执行自定义操作。