user3565144
user3565144

Reputation: 13

Unfortunately the app has stopped. What can I do to fix this?

I am programming an app. There is no errors. I can run the app, but when I press the button it says:

Unfortunately the app has stopped.

What can I do?

Here is my activity code:

Button button1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 =(Button)findViewById(R.id.button1);
    button1.setOnClickListener(this);
}

private void button1Click()
{
    startActivity (new Intent ("com.example.cp3.tutorial.Class2"));
}

public void onClick(View v) {
    switch (v.getId()) 
    {
    case R.id.button1:
        button1Click();
    }
}

and the manifest:

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.cp3.tutorial.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.cp3.tutorial.Class2"
        android:label="@string/title_activity_class2" >
    </activity>
</application>

</manifest>

Upvotes: 1

Views: 146

Answers (2)

Hamid Reza
Hamid Reza

Reputation: 664

startActivity (new Intent ("com.example.cp3.tutorial.Class2")); 

here in your code! you refer to a package to be opend. if you want to open an activity on button click use this code:

Intent intent = new Intent(firstActivity.this, secoundActivity.class);
startActivity(intent);

put this on your click listener, instead of firstActivity put your current activity and secoundActivity put your activity name you want to be opened.

Hope this help you.

Upvotes: 0

Onik
Onik

Reputation: 19959

Change button1Click() like this:

private void button1Click()
{
    startActivity (new Intent (MainActivity.this, Class2.class));
}

Upvotes: 1

Related Questions