metode onCreate tidak dipanggil di Android

Saya telah membuat 2 aktivitas. Ketika saya memanggil aktivitas kedua dari yang pertama melalui niat maka metode onCreate dari aktivitas kedua tidak dipanggil. Meskipun metode onCreate aktivitas pertama dipanggil secara normal sebagaimana mestinya. Di bawah ini adalah kode untuk aktivitas pertama.

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

    }

}

Kode untuk aktivitas kedua.

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

}

Manifes.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 sumber
comment
Ada perbedaan antara nama aktivitas Anda yang ditentukan di AndroidManinfest.xml dan di file java. Dalam manifesnya adalah ShowValueActivity sedangkan pada dasarnya adalah ShowValue.java   -  person Tanmay Mandal    schedule 14.01.2014


Jawaban (3)


kamu punya

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

alih-alih

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

Sejak kode Anda dibaca

public class ShowValue extends Activity {

Namun, mengapa aplikasi Anda tidak mogok dan mengatakan NoActivityFoundException ??

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

silakan lihat perubahan nyata

<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>

manifes Anda seharusnya seperti ini

<?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

Ubah nama Aktivitas Kedua Anda di file kelas seperti ini:

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

}

Ini akan berhasil :)

person Sonali8890    schedule 14.01.2014