0

需要一些帮助来创建我的主菜单。我已经成功制作了一个点击监听器,可以切换到不同的活动,但我无法让第二个活动正常工作。该应用程序在模拟中运行良好,但是当我点击重量杜松子酒按钮时没有任何反应。我似乎无法找到问题所在

      package com.example.bmiworking;

    import android.app.Activity;
    import android.content.DialogInterface;
    import android.view.View.OnClickListener;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainMenu extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    Button btn,btn1,btn2,btn3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);
        btn = (Button) findViewById(R.id.buttonBMI);
        btn.setOnClickListener(this);
        btn1 = (Button) findViewById(R.id.buttonGain);
        btn1.setOnClickListener(this);
    }

        @Override
    public void onClick(View v) {
        if (v.getId() == R.id.buttonBMI) {
            startActivity(new Intent(this, MainActivity.class));
        }
    }
        public void onClick1(View d) {
            if (d.getId() == R.id.buttonGain) {
                startActivity(new Intent(this, weightgain.class));
            }
        }
    }


package com.example.bmiworking;

import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class weightgain extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weightgain);
}
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".StartingPoint" 
    android:background="@drawable/grunge">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Main Menu"
        android:textSize="45dp"
        android:layout_gravity="center"
        android:gravity="center" 
        android:id="@+id/tvDisplay"/>

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="BMI Goal" 
        android:layout_gravity="center"
        android:textSize="20dp"
         android:id="@+id/bSub"/>

    <Button
        android:id="@+id/buttonBMI"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="BMI Calculator"
        android:textSize="20dp" />

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Muscle Loss Workouts" 
        android:layout_gravity="center"
        android:textSize="20dp"
         android:id="@+id/bSub"/>

    <Button
        android:id="@+id/buttonGain"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Muscle Gain Workouts"
        android:textSize="20dp" />

</LinearLayout>



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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.bmiworking.MainActivity"
            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="com.example.bmiworking.MainMenu"
            android:label="@string/app_name" >
            </activity>
            <activity
            android:name="com.example.bmiworking.weightgain"
            android:label="@string/app_name" >
            </activity>
    </application>

使用堆栈跟踪进行编辑:

  01-24 02:34:34.739: D/dalvikvm(808): newInstance failed: p0 i0 [0 a1
    01-24 02:34:34.754: D/AndroidRuntime(808): Shutting down VM
    01-24 02:34:34.754: W/dalvikvm(808): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
    01-24 02:34:34.819: E/AndroidRuntime(808): FATAL EXCEPTION: main
    01-24 02:34:34.819: E/AndroidRuntime(808): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bmiworking/com.example.bmiworking.MainMenu}: java.lang.InstantiationException: can't instantiate class com.example.bmiworking.MainMenu
    01-24 02:34:34.819: E/AndroidRuntime(808):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at android.os.Handler.dispatchMessage(Handler.java:99)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at android.os.Looper.loop(Looper.java:137)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at android.app.ActivityThread.main(ActivityThread.java:5039)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at java.lang.reflect.Method.invoke(Method.java:511)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at dalvik.system.NativeStart.main(Native Method)
    01-24 02:34:34.819: E/AndroidRuntime(808): Caused by: java.lang.InstantiationException: can't instantiate class com.example.bmiworking.MainMenu
    01-24 02:34:34.819: E/AndroidRuntime(808):  at java.lang.Class.newInstanceImpl(Native Method)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at java.lang.Class.newInstance(Class.java:1319)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
    01-24 02:34:34.819: E/AndroidRuntime(808):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
    01-24 02:34:34.819: E/AndroidRuntime(808):  ... 11 more
4

5 回答 5

1

将 onClick 代码更改为使用 switch case 而不是 if-else :

@Override
public void onClick(View v) {
 switch(v.getId()){
  case R.id.buttonBMI: 
          startActivity(new Intent(this, MainActivity.class));
       break;

  case R.id.buttonGain:  
       startActivity(new Intent(this, weightgain.class));
       break;
  }
}
于 2013-01-24T02:12:41.290 回答
1

改变它,使它像这样:

@Override
public void onClick(View v) {
  if (v.getId() == R.id.buttonBMI) {
    startActivity(new Intent(this, MainActivity.class));
  }
  if (v.getId() == R.id.buttonGain) {
    startActivity(new Intent(this, weightgain.class));
  }
}

您只会真正覆盖onClick(View v),因此,两次按下按钮都会onClick(View v)调用(onClick1()永远不会被使用)。然后因为你有一个 if,第二个按钮不会开始任何事情。当然,如果您有很多按钮,请考虑使用switch语句(如 ρяσѕρєяK 概述)。但是,想法是一样的,您只使用 1 种方法。

于 2013-01-24T02:11:20.777 回答
0

首先将 onClick 代码更改为,

@Override
public void onClick(View v) {
 switch(v.getId()){
 case R.id.buttonBMI: 
      startActivity(new Intent(MainMenu.this, MainActivity.class));
   break;

 case R.id.buttonGain:  
   startActivity(new Intent(MainMenu.this, weightgain.class));
   break;
}
}

创建新实例时不要传递这个,将它作为ActivityName.this传递

于 2013-01-24T06:29:20.907 回答
0

你可以试试这个代码

@Override
public void onClick(View v) {
  switch(v.getId()){
    case R.id.buttonBMI: 
         startActivity(new Intent(MainMenu.this, MainActivity.class));
         break;

    case R.id.buttonGain:  
        startActivity(new Intent(MainMenu.this, weightgain.class));
        break;
    }
}

或者

试试这第二种方法

public void buttonBMIClick(View view){
    startActivity(new Intent(MainMenu.this, MainActivity.class));
}

public void buttonGainClick(View view){
     startActivity(new Intent(MainMenu.this, weightgain.class));
}

让你布局 XML

<Button
    android:id="@+id/buttonBMI"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="BMI Calculator"
    android:onClick="buttonBMIClick"
    android:textSize="20dp" />

 <Button
    android:id="@+id/buttonGain"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Muscle Gain Workouts"
    android:onClick="buttonGainClick"
    android:textSize="20dp" />
于 2013-01-24T07:27:25.903 回答
0

我认为您错过了有关 Intent 和 OnClick 方法的正确实现之类的东西。请尝试将您的代码替换为我的代码,您将获得良好的结果;

@Override
    public void onClick(View v) {
        if (v.equals(btn)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        } else if (v.equals(btn1)) {
            Intent intent = new Intent(this, weightgain.class);
            startActivity(intent);
        }
    }

在上面,“ btn ”和“ btn1 ”是您的按钮。

请让我知道你的结果。

于 2013-01-24T04:21:21.390 回答