2013-07-14 144 views
0

My Code here is calling my class Round List,but when I click「Round 1」Button,nothing happened,I do not get any errors,it just does not,if just does not work,if someone have any idea why這是發生了,請你讓我知道,謝謝。Android Intent not calling class

public class MainActivity extends Activity implements OnClickListener { 
public LinearLayout layout; 
public LinearLayout roundList; 
private Button round1, round2, round3; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    ScrollView scroll = new ScrollView(this); 
    setContentView(scroll); 

    layout = new LinearLayout(this); 
    layout.setOrientation(LinearLayout.VERTICAL); 
    scroll.addView(layout); 

    TextView title = new TextView(this); 
    title.setText("Select Round"); 
    title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 40); 
    title.setPadding(6,6,6,6); 
    title.setTextColor(Color.rgb(255, 150, 0)); 
    title.setGravity(Gravity.CENTER); 
    layout.addView(title); 

    roundList = new LinearLayout(this); 
    roundList.setOrientation(LinearLayout.VERTICAL); 
    layout.addView(roundList); 

    Button round1 = new Button(this);  
    round1.setText("Round 1"); 
    round1.setPadding(15, 15, 15, 15); 
    round1.setOnClickListener(this); 
    layout.addView(round1); 

    Button round2 = new Button(this); 
    round2.setText("Round 2"); 
    round2.setPadding(15, 15, 15, 15); 
    round2.setOnClickListener(this); 
    layout.addView(round2); 

    Button round3 = new Button(this); 
    round3.setText("Round 3"); 
    round3.setPadding(15, 15, 15, 15); 
    round3.setOnClickListener(this); 
    layout.addView(round3); 

} 

@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; 
} 

public void onClick(View v) { 
    if(v == round1) { 
     Intent round = new Intent(this, RoundList.class); 
     startActivity(round); 
    } 
}//end onClick 

}

這是我RoundList類。

public class RoundList extends MainActivity implements RoundCallback { 

public RoundList() { 

    TextView subTitle = new TextView(this); 
    subTitle.setText("Results"); 
    subTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 40); 
    subTitle.setPadding(6,6,6,6); 
    subTitle.setTextColor(Color.rgb(255, 150, 0)); 
    subTitle.setGravity(Gravity.CENTER); 
    layout.addView(subTitle); 

    new GetRound(this); 
} 

public void addMatchup(JSONObject matchup) throws JSONException { 
    LinearLayout laid = new LinearLayout(this); 
    laid.setOrientation(LinearLayout.VERTICAL); 
    laid.setPadding(0, 0, 0, 20); 

    LinearLayout teams = new LinearLayout(this); 
    teams.setOrientation(LinearLayout.HORIZONTAL); 
    //teams.setWeightSum(1.0f); 

    LinearLayout teamHome = new LinearLayout(this); 
    teamHome.setOrientation(LinearLayout.VERTICAL); 
    teamHome.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT, 
      0.4f)); 

    TextView teamHomeName = new TextView(this); 
    final String teamHomeStr = matchup.getJSONObject("teamHome").getString("name"); 
    teamHomeName.setText(teamHomeStr); 
    teamHomeName.setTextAppearance(this, android.R.style.TextAppearance_Large); 
    teamHomeName.setGravity(Gravity.CENTER); 
    teamHome.addView(teamHomeName); 

    TextView teamHomeScore = new TextView(this); 
    teamHomeScore.setText(matchup.getJSONObject("result").getString("home")); 
    teamHomeScore.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
    teamHomeScore.setGravity(Gravity.CENTER); 
    teamHome.addView(teamHomeScore); 

    teams.addView(teamHome); 

    TextView vs = new TextView(this); 
    vs.setText("vs"); 
    vs.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
    vs.setTextColor(Color.RED); 
    vs.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT, 
      0.65f)); 
    vs.setGravity(Gravity.CENTER); 
    //vs.setBackgroundColor(Color.GREEN); 
    teams.addView(vs); 


    LinearLayout teamAway = new LinearLayout(this); 
    teamAway.setOrientation(LinearLayout.VERTICAL); 
    teamAway.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT, 
      0.4f)); 

    TextView teamAwayName = new TextView(this); 
    final String teamAwayStr = matchup.getJSONObject("teamAway").getString("name"); 
    teamAwayName.setText(teamAwayStr); 
    teamAwayName.setTextAppearance(this, android.R.style.TextAppearance_Large); 
    teamAwayName.setGravity(Gravity.CENTER); 
    teamAway.addView(teamAwayName); 

    TextView teamAwayScore = new TextView(this); 
    teamAwayScore.setText(matchup.getJSONObject("result").getString("away")); 
    teamAwayScore.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
    teamAwayScore.setGravity(Gravity.CENTER); 
    teamAway.addView(teamAwayScore); 

    teams.addView(teamAway); 

    laid.addView(teams); 

    final String locationStr = matchup.getString("location"); 
    TextView location = new TextView(this); 
    location.setText(locationStr); 
    location.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
    location.setPadding(0,0,0,6); 
    location.setTextColor(Color.BLUE); 
    location.setGravity(Gravity.CENTER); 
    location.setClickable(true); 
    location.setOnClickListener(this); 
    laid.addView(location); 

    final String timeStr = matchup.getString("time");  
    TextView time = new TextView(this); 
    time.setText(timeStr); 
    time.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
    time.setPadding(0,6,0,0); 
    time.setTextColor(Color.BLUE); 
    time.setGravity(Gravity.CENTER); 
    time.setClickable(true); 
    time.setOnClickListener(new OnClickListener() { 
     @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
     @Override 
     //Sends an appointment fixture to a calender 
     public void onClick(View v) { 
      Calendar beginTime = Calendar.getInstance(); 
      beginTime.set(2012, 0, 19, 7, 30); 
      Calendar endTime = Calendar.getInstance(); 
      endTime.set(2012, 0, 19, 8, 30); 

      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); 
      Date date = null; 
      try { 
       date = formatter.parse(timeStr); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 
      long dateInLong = date.getTime(); 

      Intent intent = new Intent(Intent.ACTION_INSERT) 
        .setData(Events.CONTENT_URI) 
        .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, dateInLong) 
        .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, dateInLong + 1000 * 60 * 60) 
        .putExtra(Events.TITLE, "Soccer match") 
        .putExtra(Events.DESCRIPTION, "Soccer match between " 
        + teamHomeStr + " and " + teamAwayStr) 
        .putExtra(Events.EVENT_LOCATION, locationStr) 
        .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY) 
        ; 
      startActivity(intent); 
     }}); 
    laid.addView(time); 

    roundList.addView(laid); 

    //get sms details and output to sms application 
    TextView sms = new TextView(this); 
    sms.setText("SMS Details"); 
    sms.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
    sms.setPadding(0,6,0,0); 
    sms.setTextColor(Color.DKGRAY); 
    sms.setGravity(Gravity.CENTER); 
    sms.setClickable(true); 
    sms.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
     String uri= "smsto:"; 
     Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri)); 
     intent.putExtra("sms_body", "\"" + teamHomeStr + " vs " + teamAwayStr + "\n" + locationStr + "\n" + timeStr + "\""); 
     intent.putExtra("compose_mode", true); 
     startActivity(intent); 
     }}); 
    laid.addView(sms); 
} 

@Override 
public void roundCallback(JSONObject obj) { 
    JSONArray matchups; 
    try { 
     matchups = obj.getJSONArray("matchups"); 

     for (int i = 0; i < matchups.length(); i++) { 
      JSONObject matchup = matchups.getJSONObject(i); 

      addMatchup(matchup); 

     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
//Locates the location using Google maps. 
public void onClick(View obj) { 
    if (obj instanceof TextView) { 
     TextView txt = (TextView) obj; 
     String uri = "geo:0,0?q=" + txt.getText().toString().replace(' ', '+'); 
     startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri))); 
    } 
} 

}

回答

4

你的代碼應該是

round1 = new Button(this); 
round2 = new Button(this); 
round3 = new Button(this); 

,而不是

Button round1 = new Button(this); 
Button round2 = new Button(this); 
Button round3 = new Button(this); 
在你的代碼

你都躲在memember類round1round2round3 ridefining那些在onCreate me的範圍thod

+0

現在按鈕點擊的作品,但它只是錯誤 – thechrishaddad

+0

我明白了。爲什麼RoundClass擴展MainActivity而不是Activity? – Blackbelt

+0

我不好,我改變了。 – thechrishaddad

0

單擊按鈕時未啓動另一個活動的原因是因爲您在OnClick()方法中檢查的按鈕是never clicked

您正在創建local button object round1 in OnCreate()方法,而不是實例化整個班級可見的按鈕對象round1。因此,當OnCreate()方法超出範圍時,本地按鈕對象也會被垃圾收集器銷燬並收集。

要解決此問題,請在round1,round2, round3 in OnCreate()方法之前刪除Button並解決您的問題。

相關問題