2014-01-19 51 views
-2
public class MainActivity extends Activity implements OnClickListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.first_screen); 
     ImageButton x = (ImageButton) findViewById(R.id.imageButton1); 
     x.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     Intent intent = new Intent(
       com.example.choiceisaseriousmatter.MainActivity.this, 
       Choice.class); 
     startActivity(intent); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is 
     // present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

此代碼用於在單擊imageButton時切換到另一個Activity。但是每當我點擊按鈕時,程序就會崩潰。如何使用意圖切換到另一個活動?

+0

顯示您的崩潰日誌。 –

+0

請發佈LogCat中的錯誤。 – jagmohan

+1

在'onClick'函數之前添加'@ Override'。既然你正在實施它,你可能需要重寫。 –

回答

1

您是否已經覆蓋了onClick方法?嘗試此更改:

@Override 
public void onClick(View v) { 
    Intent intent = new Intent(MainActivity.this, Choice.class); 
    startActivity(intent); 
} 
相關問題