2012-02-03 59 views
0

有人可以告訴我我錯了。我有三個活動,我想連接到轉換器。這段代碼是我的第一個java文件和第一個xml文件。我認爲我的onclick代碼在哪裏是不正確的。我的最終結果是,所有3個活動連接3個圖像按鈕...謝謝連接活動

java 1.code package my.hope;

import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.ImageView; 
    import android.content.Intent; 
    public class NewhopeActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ImageView myImage = (ImageView) findViewById(R.id.imageButton1); 
    myImage.setOnClickListener(new OnClickListener() { 

     intent intent = new intent(Newhopeactivity.this, Act2.class); 
     startActivity(intent); 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 

      } 
    } 
); 
} 

}

xml.code

<?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:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/bt" /> 

<ImageButton 
    android:id="@id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" 
    android:onClick="Act2"/> 

回答

1

你必須把startActivity()到實際的onClick()方法。

myImage.setOnClickListener(new OnClickListener() { 


     public void onClick(View v) { 
      intent intent = new intent(Newhopeactivity.this, Act2.class); 
      startActivity(intent); 
     } 
} 
0

變化

ImageView myImage = (ImageView) findViewById(R.id.imageButton1); 
myImage.setOnClickListener(new OnClickListener() { 

    intent intent = new intent(Newhopeactivity.this, Act2.class); 
    startActivity(intent); 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
} 

ImageView myImage = (ImageView) findViewById(R.id.imageButton1); 
myImage.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub   
      Intent intent = new intent(Newhopeactivity.this, Act2.class); 
      startActivity(intent); 
     } 
} 
+0

謝謝你這麼多它爲我工作。我對所有三項活動都做了相同的工作,它的工作原理是什麼,在所有三個活動頁面上放一個按鈕,以便選擇返回 – ravon30 2012-02-03 23:54:43