在同一个活动中放置多个onClickListener和OnClick

时间:2014-11-10 17:52:22

标签: java android

我正在尝试设置多个按钮,但我似乎无法做到这一点。我在同一个活动中有2个onClickListener函数。我这样做是对的,还是应该以其他方式完成? btnChpt3工作但是当我为btnChpt3_1输入onclicklistener时,它会在打开时立即关闭。感谢。

MainMenu.java

 package com.th3ramr0d.learnar670_1;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;

 public class MainMenu extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button chapterThree = (Button) findViewById(R.id.btnChpt3); 

    chapterThree.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            startActivity(new Intent(MainMenu.this, Chapter3.class));

        }
    });


    Button chapterThree_1 = (Button) findViewById(R.id.btnChpt3_1);

    chapterThree_1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            startActivity(new Intent(MainMenu.this, Chapter3_1.class));

        }
    });

 }


 @Override
 protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
 }

 }

所以我添加了一个新类来处理子菜单页面上的按钮。但是现在我遇到的问题是我以前按钮不会打开的问题。我猜它是因为代码不是出于某种原因运行。

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.th3ramr0d.learnar670_1"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainMenu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

   <activity
        android:name=".SubMenuChapter3"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Chapter3"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        </activity>



    <activity
        android:name=".Chapter4"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.th3ramr0d.learnar670_1.CHAPTER4" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        </activity>


 </application>

 </manifest>

你可以在这里看到我的.MainMenu发布。我猜这意味着它在启动时启动。按钮有效。但是,现在我想要与.SubMenuChapter3相关联的按钮启动,它什么都不做。在这里Program runs with no errors but button wont open你可以看到我遇到了同样的问题,直到codepg足够好告诉我哪里出错了。但是我想使用其中几个子菜单,那么我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

我可以从您的评论中看到您正在膨胀视图activity_main但是ID为bthChpt3_1的Button位于另一个文件中。这是一个错误,因为您可以使用findViewById(R.id.buttonId)来查找您正在使用的视图中存在的按钮的ID。由于该按钮位于另一个xml文件中,因此它将抛出空指针异常。

答案 1 :(得分:1)

在阅读之后,您的问题是您正在引用您在另一个不会膨胀的布局中创建的按钮。

setContentView(R.layout.activity_main);中你正在膨胀activity_main,因此你只能拥有该xml中按钮和元素的onclick监听器。

确保在此xml中添加btnChpt3,或者如果此按钮也应该在另一个布局中,则通过对其进行膨胀来设置另一个活动或片段控件。

现在这个方法Button chapterThree = (Button) findViewById(R.id.btnChpt3);正在返回空指针异常。