第二个活动在第一个活动启动意图后显示空白屏幕

4

你好,我正在尝试使用隐式意图来启动第二个活动。TextView和Button在Activity One中起作用,但是当我点击按钮启动第二个活动时,在第二个活动中看不到任何TextView。我只得到一个黑色的空白屏幕。如果我按返回键,它会带我回到Activity One。

我也只是按照这个链接做了一个测试: http://mubasheralam.com/tutorials/android/how-start-another-activity

结果同样发生了。我可以看到Activity One,但Activity Two只是一个空白屏幕。

这是我的代码:

ActivityOne.java:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.LinearLayout;


public class ActivityOne extends Activity implements OnClickListener {
    private TextView mytext;
    private Button mybutton;
    private LinearLayout linearlayout;    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        linearlayout = new LinearLayout(this);
        mytext = new TextView(this);
        mybutton = new Button(this);
        mybutton.setText("Next Activity");
        mybutton.setOnClickListener(this);

        mytext.setText("Activity1");
        linearlayout.addView(mytext);
        linearlayout.addView(mybutton);

        setContentView(linearlayout);

    }

    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setAction("com.example.hello.SHOW");
        intent.putExtra("com.example.hello.Implicit_intent", "This is extras");
        startActivity(intent);
    }

}

ActivityTwo.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ActivityTwo extends Activity {
    private TextView mytext;
    private LinearLayout linearlayout;

    public void OnCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mytext = new TextView(this);
        linearlayout = new LinearLayout(this);

        linearlayout.addView(mytext);

        Bundle extras = getIntent().getExtras();
        mytext.setText("Activity One value: " + extras.getString("com.example.hello.Implicit_intent"));
        setContentView(linearlayout);
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.hello"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityOne"
                  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=".ActivityTwo"
                  android:label="@string/app_name">
            <intent-filter>
                    <action android:name="com.example.hello.SHOW" />
                    <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>
</manifest>

我正在使用 Android 模拟器 2.3.3,CPU/ABI 是 ARM。

我看到你在清单文件中引用了“ActivityTwo”,但是在源代码中它被称为“OtherActivity”。这可能是问题所在吗? - pents90
哦,我刚才打错了,所以那不是问题。谢谢你注意到了。我已经编辑了我的帖子。 - sharkfin
在文本设置后,向您的第二个活动添加一个Log.e语句。通过system.out或Log.e测试检索到的额外字符串。 - coder_For_Life22
我拼错了 "onCreate()"。 我本不该使用大写字母'O'。 - sharkfin
我遇到了同样的问题,但我的onCreate被正确调用了。第二个布局中的按钮没有显示出来,如果我使用findViewById则返回null。 - Bagusflyer
显示剩余3条评论
1个回答

0
你可以尝试在ActivityTwo中为TextView和LinearLayout添加布局参数,例如:
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ViewGroup.LayoutParams tParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

    ViewGroup.LayoutParams lParams = new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT);

    mytext = new TextView(this);
    linearlayout = new LinearLayout(this);

    mytext.setLayoutParams(tParams);
    linearlayout.setLayoutParams(lParams);

    linearlayout.addView(mytext);

    Bundle extras = getIntent().getExtras();
    mytext.setText("Activity One value: " + 
            extras.getString("com.example.hello.Implicit_intent"));
    setContentView(linearlayout);
}

2
谢谢。实际上我已经得到了答案。我拼错了“onCreate()”。我使用了大写字母'O'。 - sharkfin
我也犯了一个大写字母'O'的错误,这个真的帮了我很多。 - Kgrover

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接