【问题标题】:onCreate method not called in androidandroid中未调用onCreate方法
【发布时间】:2014-01-14 06:17:49
【问题描述】:

我创建了 2 个活动。当我通过意图从第一个活动调用第二个活动时,第二个活动的 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>

【问题讨论】:

  • AndroidManinfest.xml 和 java 文件中定义的活动名称存在差异。在清单中它是 ShowValueActivity 基本上是 ShowValue.java

标签: android android-intent android-manifest


【解决方案1】:

你有

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

【讨论】:

    【解决方案2】:

    请查看清单 改变

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

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

    【讨论】:

      【解决方案3】:

      在类文件中更改您的第二个活动名称,如下所示:

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

      它会起作用的:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多