开发者

Second Activity shows blank screen after first Activity starts Intent

Hi I'm trying to use an implicit intent to start a second activity. The TextView and Button work for Activity One, but when I click the button to start the second activity, I do not see any of the TextViews in the second activity. All I get is a black blank screen. If I press back, it takes me back to Activity One.

I also followed this just as a test: http://mubasheralam.com/tutorials/android/how-start-another-activity

And same thing happens. I can see Activity One, but Activity Two is just a blank screen.

Here is my code:

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>

I'm using Android Emulator 2.3.3, CPU/ABI is ARM


You could try adding layout params to the textview and linear layout in ActivityTwo like

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜