onCreate方法没有在android中调用

Shi*_*pta 0 android android-manifest android-intent

我已经创建了2个活动.当我从第一个通过intent调用第二个活动时,第二个活动的onCreate方法没有被调用.尽管第一个活动的onCreate方法正常调用它应该是. 以下是第一个活动的代码.

package com.webesperto.webespertofirstapp;

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.TextView;

public class MainActivity extends Activity implements OnClickListener {

    int counter;
    Button add, sub;
    TextView tView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        counter = 0;
        add = (Button) findViewById(R.id.bAdd);
        sub = (Button) findViewById(R.id.bSub);
        tView = (TextView) findViewById(R.id.textView1);

        // Intent in = new Intent(AC);

        add.setOnClickListener(this);

        sub.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bAdd:         
            tView.setText(++counter + "");
            try {               
            Intent nextIntentView = new Intent(MainActivity.this, ShowValueActivity.class);
            nextIntentView.putExtra("key", "counter");
            startActivity(nextIntentView);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            break;

        case R.id.bSub:
            tView.setText(--counter + "");
            break;
        default:
            break;
        }   

    }

}
Run Code Online (Sandbox Code Playgroud)

第二项活动的代码.

package com.webesperto.webespertofirstapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ShowValue extends Activity {

    TextView tv_showValue1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_value);
        tv_showValue1 = (TextView) findViewById(R.id.tv_showValue);
        Intent gotIntent = (Intent) this.getIntent();
        Bundle gotBundle = gotIntent.getExtras();
        tv_showValue1.setText(gotBundle.getString("key"));
    }

}
Run Code Online (Sandbox Code Playgroud)

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.webesperto.webespertofirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.webesperto.webespertofirstapp.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=".ShowValueActivity"
            android:label="@string/title_activity_show_value" >
        </activity>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

Rat*_*lle 7

你有

   <activity
        android:name=".ShowValueActivity"
        android:label="@string/title_activity_show_value" >
    </activity>
Run Code Online (Sandbox Code Playgroud)

代替

<activity
        android:name=".ShowValue"
        android:label="@string/title_activity_show_value" >
    </activity>
Run Code Online (Sandbox Code Playgroud)

由于您的代码读取

public class ShowValue extends Activity {
Run Code Online (Sandbox Code Playgroud)

虽然,为什么你的应用程序崩溃说NoActivityFoundException