2011-09-25 92 views
2

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

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

而且同样的事情发生。我可以看到活动一,但活动二只是一个空白的屏幕。

这里是我的代码:

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 i的ARM

+0

我看你是指“ActivityTwo”的清单,但在源代码中它被称为“OtherActivity ”。这可能是问题吗? – pents90

+0

哦,我只是在这里输入错误,所以这不是问题。谢谢你抓住那个。我编辑了我的帖子。 – sharkfin

+0

设置文本后,将Log.e语句添加到第二个活动。测试您通过system.out或Log.e检索的额外字符串。 –

回答

0

你可以尝试添加布局PARAMS在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); 
} 
+1

谢谢。其实我只是得到了答案。我拼错了“onCreate()”。我用了大写'O' – sharkfin

+0

没有看到那一个。很好,你发现你的自我。 – blessenm

+0

我与首都'O'有同样的错误。这真的有帮助。 – Kgrover

相关问题