所以我希望能够从主菜单中启动多个活动。所有按钮都有android:onClick =" onClick"在每个单独的按钮。但是,当我试图让按钮执行从单击启动特定活动的意图时,没有任何反应。 如何启用按钮以启动不同的活动? 这是我的代码:
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,按钮确实尝试点击下一个活动。但是只有前程万里(MileageKeeper)按钮才能工作并打开前程万里(Mileage Keeper)活动。其他按钮会导致应用关闭。所以我猜测我把意图放在了Manifest中。所以这是我的清单:
<?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>
答案 0 :(得分:2)
从xml中删除android:onClick="onClick"
,它将起作用。