user3900101
user3900101

Reputation: 53

How do I enable multple buttons to launch specific activities

So I want to be able to launch multiple activities from my main menu. All the buttons have android:onClick="onClick" in each individual button. However when I am trying to get the buttons to do the intent to start the specific Activity from the click, nothing happens. How do I enable my buttons to start the different Activities? Here's my code:

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;
     }

   };
}

Now that I have removed the extra onClick, the buttons does try to click to the next activity. However only the MileageKeeper button works and opens the Mileage Keeper activity. The other buttons cause the app to close. So I'm guessing I put the intents in the Manifest wrong. So here is my 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>

Upvotes: 0

Views: 79

Answers (1)

Kosh
Kosh

Reputation: 6334

remove android:onClick="onClick" from your xml and it shall works.

Upvotes: 2

Related Questions