2012-03-02 145 views
-2

我正嘗試從一項活動轉移到另一項活動上的簡單Android應用程序代碼。但它似乎並沒有工作。一旦我點擊按鈕「更改活動」,該應用程序停止工作。這是兩個活動的代碼。在Android上從一項活動轉移到另一項活動

1.主要活動。所述一個用戶看到第一時,應用程序是向上

package eg.edu.guc; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class HelloAndroidActivity extends Activity { 
    Button b1; 
    TextView t1; 
    EditText e1; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     b1 = (Button) findViewById(R.id.button1); 
     b1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent intent = new Intent(HelloAndroidActivity.this,HelloAndroidActivity2.class); 
       HelloAndroidActivity.this.startActivity(intent); 
       //finish(); 
      } 
     }); 



     t1 = (TextView) findViewById(R.id.TextView1); 
     e1= (EditText) findViewById(R.id.editText1); 

    } 
} 
,按鈕「改變活動」將用戶帶到

2.第二活動:

package eg.edu.guc; 

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

public class HelloAndroidActivity2 extends Activity{ 
    TextView t2; 
    ImageView I; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 
     t2 = (TextView) findViewById(R.id.TextView2); 
     I = (ImageView) findViewById(R.id.imageView1); 
     I.setImageResource(R.drawable.kareem); 
    } 

} 

3.Here是主要的XML UI對於第一個活動

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <TextView 
     android:id="@+id/TextView1" 
     android:layout_marginTop="15dp" 
     android:layout_marginBottom="15dp" 
     android:layout_width="211dp" 
     android:layout_height="wrap_content" 
     android:text="Hello Android" 
     android:textSize="30dp" /> 



    <EditText 
     android:id="@+id/editText1" 
     android:layout_marginTop="15dp" 
     android:layout_marginBottom="15dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_marginTop="15dp" 
     android:layout_marginBottom="15dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Change Activity" /> 

</LinearLayout> 

4.And終於在這裏是第二個活動

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/TextView2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Kareem's Photo" > 

     <requestFocus /> 
    </TextView> 



    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.21" 
     android:src="@drawable/ic_launcher" /> 

</LinearLayout> 
的UI XML文件
+4

我認爲你缺少清單文件爲您的活動HelloAndroidActivity2條目..... – 2012-03-02 09:46:38

回答

-1

嘗試Shashank的建議。如果它不工作,試試這個:

變化

 HelloAndroidActivity.this.startActivity(intent); 

爲:

 startActivity(intent); 
1

你導入新的活動到AndroidManifest.xml中? 你應該插入水木清華像 <activity android:name=".HelloAndroidActivity2"></activity> <application></application>之間

相關問題