0

I am self learner of android and i am just trying to learn it with how i can deal with two activities and 3 and so on , for that i just make the sample code , it run fine with the one activity but when i run it for two activities it show me force stop error , i also mention second activity in my manifest.xml

manifest.xml

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.predictor.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.predictor.activity2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN1" />

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

Here is mainactivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View btn_activity_1 = findViewById(R.id.btn_activity_1);
        btn_activity_1.setOnClickListener((OnClickListener) this);


    }
    public void onClick(View v) { 
        switch (v.getId()) { 
        case R.id.btn_activity_1:
            Intent k = new Intent(MainActivity.this, activity2.class);
            startActivity(k);
            //finish();
            break;
        }

        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
AHF
  • 1,070
  • 2
  • 15
  • 47
  • 4
    It might help to post your LogCat so we can see what the error is. – durbnpoisn Jun 05 '14 at 20:45
  • 3
    You should remove the intent-filter in the 2nd activity in your manifest. And if you want to keep it, then change MAIN1 with MAIN. The action MAIN1 is not existing. Maybe it is not the only problem, the logs should help to debug your app. – eternay Jun 05 '14 at 20:46
  • i just started android today , i dont know about how to work with logcat but letme find it out – AHF Jun 05 '14 at 20:46
  • 1
    check this for learning how to debug android: http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – donfuxx Jun 05 '14 at 20:48
  • @eternay i solve it but still problem exist leme post my logcat – AHF Jun 05 '14 at 20:51
  • BEFORE posting your logcat, take a close look at it, and do google-searches on the exception(s) and keywords you might see. It'll help you **much** more in the long run if you try resolving these issues before asking here. – 323go Jun 05 '14 at 20:57
  • Delete the from the second activity. You're only supposed to write this on the activity that would be launched when the app starts. Alsom, MAIN1 doesn't exist – DDsix Jun 05 '14 at 20:57

2 Answers2

1

You should change :

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

as

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

in second activity in xml .

canmurat
  • 159
  • 11
0

I think I've seen your error: you are casting this to OnClickListener, but your class is not implementing the OnClickListener interface. You should have something like:

public class MainActivity extends ActionBarActivity implements OnClickListener {
   // Your code for this class
}

It's difficult to say if it's the only problem, you should post your logs.

eternay
  • 3,754
  • 2
  • 29
  • 27