开发者

startActivity makes the app force close

开发者 https://www.devze.com 2023-03-21 17:10 出处:网络
I just started development for Android, and I am stuck with this code: public class Home extends Activity {

I just started development for Android, and I am stuck with this code:

public class Home extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        Button buttonFoo = (Button)findViewById(R.id.buttonFoo);
        buttonFoo.setOnClickListener(openFoo);
    }

    public OnClickListener openFoo = new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(Home.this, Foo.class);
            startActivity(intent);
        }
    };
}

And here is the code of activity Foo:

public class Foo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //populateWithLatest();
    }
}

And the error log:

07-18 19:53:35.043: ERROR/AndroidRuntime(4656): FATAL EXCEPTION: main
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.github.progval.openquote/com.github.progval.openquote.sites.FooActivity}; have you declared this activity in your AndroidManifest.xml?
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.app.Instrumentation.checkStartActivityRes开发者_如何学Cult(Instrumentation.java:1405)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.app.Activity.startActivityForResult(Activity.java:2827)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.app.Activity.startActivity(Activity.java:2933)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at com.github.progval.openquote.Home$1.onClick(Home.java:34)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.view.View.performClick(View.java:2501)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.view.View$PerformClick.run(View.java:9107)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.os.Handler.handleCallback(Handler.java:587)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.os.Looper.loop(Looper.java:130)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at android.app.ActivityThread.main(ActivityThread.java:3835)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at java.lang.reflect.Method.invoke(Method.java:507)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656):     at dalvik.system.NativeStart.main(Native Method)

Unless I comment the line startActivity(intent);, the app force closes when I press the Foo button.


<application android:label="@string/app_name" android:icon="@drawable/icon">
    <activity android:name=".Home"
              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=".Foo"></activity>

This is my favorite part:

07-18 19:53:35.043: ERROR/AndroidRuntime(4656): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.github.progval.openquote/com.github.progval.openquote.sites.VdmActivity}; have you declared this activity in your AndroidManifest.xml?

I don't know if this is the best way to implement a button, but here's how I do it:

findViewById(R.id.buttonFoo).setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(v.getContext(), Foo.class);
        startActivity(i);
    }
});


See your manifest File.You didn't Sign Foo Activity there.Under the main Activity Tag

put < activity android:name=".Foo">< /activity>


All activities need to be declared in the manifest file. Check there first.


It's easier and your code will look better when you implements OnClickListener. So, you just have to add the method onClick(View v). As context for your onClickListener set 'this' and also for your context of the intent. See the code below. Normally, this will work.

public class Home extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        Button buttonFoo = (Button)findViewById(R.id.buttonFoo);
        buttonFoo.setOnClickListener(this);
    }

    public void onClick(View v) {
        Intent intent = new Intent(this, Foo.class);
        startActivity(intent);
    }
}

EDIT: I also for your Foo Activity, there is a leading dot by declaring the activity in your manifest. Remove this to find your Activity. The best way to declare your Activity in the manifest is with GUI (Android Manifest File > Application > Application Nodes > Add > Activity > Name : select your activity

Have fun with coding in Android! Grtz


Your Foo class should setContentView. That is,

public class Foo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Foo.setContentView(R.layout.????)
        //populateWithLatest();
    }
}

Otherwise

0

精彩评论

暂无评论...
验证码 换一张
取 消