Android 如何启用多个按钮来启动特定活动
因此,我希望能够从主菜单启动多个活动。所有按钮在每个单独的按钮中都有android:onClick=“onClick”。但是,当我试图让按钮执行从单击开始特定活动的意图时,什么也没有发生。 如何启用按钮来启动不同的活动? 这是我的密码:Android 如何启用多个按钮来启动特定活动,android,button,android-intent,android-manifest,buttonclick,Android,Button,Android Intent,Android Manifest,Buttonclick,因此,我希望能够从主菜单启动多个活动。所有按钮在每个单独的按钮中都有android:onClick=“onClick”。但是,当我试图让按钮执行从单击开始特定活动的意图时,什么也没有发生。 如何启用按钮来启动不同的活动? 这是我的密码: import com.customerautomotivenetwork.RssFeed.VehicleRecall; import com.customerautomotivenetwork.vehicleinfo.VehicleInfo; import co
import com.customerautomotivenetwork.RssFeed.VehicleRecall;
import com.customerautomotivenetwork.vehicleinfo.VehicleInfo;
import com.customerautomotivenetwork.MileageKeeper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button toMileageKeeeper = (Button) findViewById(R.id.toMileageKeeper);
toMileageKeeeper.setOnClickListener(this);
Button toVehicleInfo = (Button) findViewById(R.id.toVehicleInfo);
toVehicleInfo.setOnClickListener(this);
Button toVehicleRecall = (Button) findViewById(R.id.toVehicleRecall);
toVehicleRecall.setOnClickListener(this);
//Allows the user to pick which button to pick in order to pick which activity
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.toMileageKeeper:
startActivity(new Intent(MainActivity.this,MileageKeeper.class));
break;
case R.id.toVehicleInfo:
startActivity(new Intent(MainActivity.this,VehicleInfo.class));
break;
case R.id.toVehicleRecall:
startActivity(new Intent(MainActivity.this,VehicleRecall.class));
break;
}
};
}
现在,我已经删除了额外的onClick,按钮确实尝试单击到下一个活动。但是,只有里程保持器按钮起作用并打开里程保持器活动。其他按钮导致应用程序关闭。所以我猜我把意图放错了。这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.customerautomotivenetwork"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".MileageKeeper"
android:label="Mileage Keeper">
</activity>
<activity
android:name=".VehicleInfo"
android:label="Vehicle Info">
<intent-filter>
<action android:name="com.customerautomotivenetwork.vehicleinfo.VehicleInfo" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity
android:name=".VehicleRecall"
android:label="Vehicle Recall">
<intent-filter>
<action android:name="com.customerautomotivenetwork.RssFeed.VehicleRecall" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
从xml中删除android:onClick=“onClick”,它将正常工作 您不需要在Xml中设置onClick,为什么有两个onClick?从代码中删除空的onClick方法。是的,我应该注意到这一点,但只有MileageKeeper按钮起作用。其他两个按钮导致应用程序停止。当按钮导致应用程序关闭时,它一定是在logCat中给出了一些错误,您是否也可以共享logCat?请稍候。。您的两个活动如何成为默认活动?车辆信息和车辆呼叫?