2011-09-25 79 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

相關問題