วิธีการ onCreate ไม่ได้ถูกเรียกใน Android

ฉันได้สร้างกิจกรรมไว้ 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;
        }   

    }

}

รหัสสำหรับกิจกรรมที่สอง

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"));
    }

}

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>

person Shivang Gupta    schedule 14.01.2014    source แหล่งที่มา
comment
มีความแตกต่างระหว่างชื่อกิจกรรมของคุณที่กำหนดไว้ใน AndroidManinfest.xml และในไฟล์ java ในไฟล์ Manifest มันคือ ShowValueActivity โดยที่มันคือ ShowValue.java   -  person Tanmay Mandal    schedule 14.01.2014


คำตอบ (3)


คุณมี

   <activity
        android:name=".ShowValueActivity"
        android:label="@string/title_activity_show_value" >
    </activity>

แทน

<activity
        android:name=".ShowValue"
        android:label="@string/title_activity_show_value" >
    </activity>

เนื่องจากรหัสของคุณอ่าน

public class ShowValue extends Activity {

แม้ว่าเหตุใดแอปพลิเคชันของคุณจึงไม่ขัดข้องโดยบอกว่า NoActivityFoundException ??

person Rat-a-tat-a-tat Ratatouille    schedule 14.01.2014

โปรดดูการเปลี่ยนแปลงอย่างชัดแจ้ง

<activity
        android:name=".ShowValueActivity"
        android:label="@string/title_activity_show_value" >
    </activity>

to

<activity
        android:name=".ShowValue"
        android:label="@string/title_activity_show_value" >
    </activity>

รายการของคุณควรจะเป็นแบบนี้

<?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=".ShowValue"
        android:label="@string/title_activity_show_value" >
        </activity>
    </application>

</manifest>
person Jitesh Upadhyay    schedule 14.01.2014

เปลี่ยนชื่อกิจกรรมที่สองของคุณในไฟล์คลาสดังนี้:

package com.webesperto.webespertofirstapp;

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

public class ShowValueActivity 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"));
}

}

มันจะทำงาน:)

person Sonali8890    schedule 14.01.2014