第一个Activity在第一个Activity启动Intent后显示空白屏幕

问题描述 投票:2回答:1

嗨,我正在尝试使用隐式意图来启动第二个活动。 TextView和Button适用于Activity One,但是当我单击按钮开始第二个活动时,我在第二个活动中看不到任何TextView。我得到的只是一个黑色的空白屏幕。如果我按回去,它会把我带回活动一。

我也跟着这个测试:http://mubasheralam.com/tutorials/android/how-start-another-activity

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

这是我的代码:

activity one.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);
    }

}

activity two.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 Emulator 2.3.3,CPU / ABI是ARM

android android-layout android-emulator
1个回答
0
投票

你可以尝试在ActivityTwo中添加布局参数到textview和线性布局

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.