简体   繁体   中英

Trying to switch between activities, don't know why this isn't working?

Here is the code where I add the listener to the button in my activity file:

get_started_imready = (Button) findViewById(R.id.im_ready);
        get_started_imready.setOnClickListener(new View.OnClickListener() {     
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Log.v("BUTTON ACTIVITY ENTERED","hi");
                startActivity(new Intent(ThirdActivity.this, SetupPageOne.class));
                finish();
            }
        });

Here is the button code in the corresponding xml file:

 <Button
    android:id="@+id/im_ready"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:clickable="true"
    />

And here is the code of my second activity: SetupPageOne

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class SetupPageOne extends ActionBarActivity {

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

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

When I try to run this code on my android device, the program runs without any runtime errors but the button itself is unresponsive and doesn't switch to the second activity.

Anyone have any idea? Thanks!

get_started_imready = (Button) findViewById(R.id.im_ready);
get_started_imready.setOnClickListener(new View.OnClickListener() {     
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //Log.v("BUTTON ACTIVITY ENTERED","hi");
        startActivity(new Intent(getActivity(), SetupPageOne.class));
        finish();
    }
});

use getActivity() or getApplication() instead of ThirdActivity.this because android is thread sensitive you can not refer to an Activity thread from a UI thread.

Sometimes it helps to be extremely specific to what activity the intent is coming from and going to.

What I mean by this is, when you create the new intent, include the entire package path to both activities:

Intent intent = new intent(com.example.packageone.ThirdActivity.this, com.example.packagetwo.SetupPageOne.class);

Also, if you want to double check if the code is getting to this intent (ie there really is an OnClickListener), before you create this intent to the new Activity, make a Toast!

Toast.makeText(getApplicationContext(), "buttonName is being clicked!", Toast.LENGTH_LONG).show();

Hope this helps, best of luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM