0

我的代码在这里调用我的类 Round List,但是当我单击“Round 1”按钮时,没有任何反应,我没有收到任何错误,它只是不起作用,如果有人知道为什么会发生这种情况,请告诉我知道,谢谢。

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

2 回答 2

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 类round1,在方法范围内使用这些类round2round3onCreate

于 2013-07-14T10:12:40.033 回答
0

单击按钮时未启动另一个活动的原因是因为您正在检查OnClick()方法的按钮是never clicked.

您正在创建一个local button object round1 in OnCreate()方法,而不是实例化round1整个类可见的按钮对象。所以当 OnCreate() 方法超出范围时,本地按钮对象也会被垃圾收集器销毁和收集。

要解决此问题,只需删除Button之前round1,round2, round3 in OnCreate()的方法,您的问题就应该得到解决。

于 2013-07-14T10:27:00.797 回答