user1144349
user1144349

Reputation: 85

App force close due to uncaught exception

I'm new to Java so, I think in order to prevent this error from appearing I'll have to write a Handler thread? Using try and catch? How do i do this?

Here's the code to help you out.

Thanks :)

package android.app;
import android.app.Activity;
import android.app.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidActivity extends Activity{

     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button next = (Button) findViewById(R.id.Start_CoolWhapp);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), activity2.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }
}

Upvotes: 0

Views: 398

Answers (1)

Graham Smith
Graham Smith

Reputation: 25757

Ok so there are a few things

First make sure all activities are in your manifest file like so:

<activity android:name=".AndroidActivity " android:label="@string/activity_name"></activity>
<activity android:name=".activity2" android:label="@string/another_activity_name"></activity>

You may need more attributes than that but that's the basics.

I do not know why you need "startActivityForResult" as I cannot see the code but if it is just another activity with a layout with Views then you can alter your code like so:

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent); //you do not have to use CurrentActivity.this to use startActivity however I am trying to show where context is coming from

Make sure activity2 has a layout and that the layout exists.

Upvotes: 1

Related Questions