2014-10-30 94 views
0

我是新來的編碼,並試圖在我的主要活動頁面有一系列按鈕時在項目上工作。從這些按鈕我希望每個人打開不同的活動或命令。我一直在尋找,發現我認爲應該工作,但是,它沒有。當它加載應用程序,然後點擊按鈕時,我會崩潰。以下是代碼。任何指示我的錯誤在某處會受到讚賞。多個按鈕w /每個選擇一個不同的活動

package com.example.finalproject2; 


import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Initialize Buttons 
    Button b1 = (Button) findViewById(R.id.button1); 
    Button b2 = (Button) findViewById(R.id.button02); 
    Button b3 = (Button) findViewById(R.id.button03); 
    Button b4 = (Button) findViewById(R.id.button01); 
    Button b5 = (Button) findViewById(R.id.button04); 

    //Set OnCLickListeners 
    b1.setOnClickListener(chicagoListener); 
    b2.setOnClickListener(sanJoseListener); 
    b3.setOnClickListener(baltimoreListener); 
    b4.setOnClickListener(westPalmBeachListener); 
    b5.setOnClickListener(websiteListener); } 

private OnClickListener chicagoListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     startActivity(new Intent(MainActivity.this, Chicago.class)); 
    } 
}; 
private OnClickListener sanJoseListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     startActivity(new Intent(MainActivity.this, SanJose.class)); 
    } 
}; 

private OnClickListener baltimoreListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     startActivity(new Intent(MainActivity.this, Baltimore.class)); 
    } 
}; 
private OnClickListener westPalmBeachListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     startActivity(new Intent(MainActivity.this, WestPalmBeach.class)); 
    } 
}; 
private OnClickListener websiteListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/"))); 
    } 
}; 




     { 
} 





} 
+0

發表您的logcat的,所以我們可以計算出WATS去錯了! – Panther 2014-10-30 04:21:02

回答

1

提起你的所有活動在您的應用程序宣言文件(像這樣):

<activity 
     android:name="com.example.finalproject2.Chicago" 
     android:label="@string/app_name" > 
</activity> 

並嘗試做財產以後這樣的:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    b1.setOnClickListener(this); 
    b2.setOnClickListener(this); 
    b3.setOnClickListener(this); 
    b4.setOnClickListener(this); 
    b5.setOnClickListener(this); 
} 



@Override 
public void onClick(View v) { 
    switch(v.getId()){ 
    case R.id.button1: 
     startActivity(new Intent(MainActivity.this, Chicago.class)); 
     break; 

    case R.id.button02: 
     startActivity(new Intent(MainActivity.this, SanJose.class)); 
     break; 

    case R.id.button03: 
     startActivity(new Intent(MainActivity.this, Baltimore.class)); 
     break; 

    case R.id.button01: 
     startActivity(new Intent(MainActivity.this, WestPalmBeach.class)); 
     break; 

    case R.id.button04: 
     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/"))); 
     break; 

    } 
} 
相關問題