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)));
}
}
}
現在按鈕點擊的作品,但它只是錯誤 – thechrishaddad
我明白了。爲什麼RoundClass擴展MainActivity而不是Activity? – Blackbelt
我不好,我改變了。 – thechrishaddad