简体   繁体   中英

Using Android button to open new class/activity

I have created a text button on my activity_main that when clicked I want to open a new xml called gallery. I have created an xml and class for this. When I click the button it doesn't take me anywhere.

activity_main.xml

<Button android:text="@string/Gallery"
    android:id="@+id/Button01"
    android:layout_width="200dp"
    android:textSize="28sp"
    android:background="@color/black"
    android:textColor="@color/pink"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true">
</Button>

android manifest

  <activity android:name=".Gallery"
        android:label="@string/app_name" />

two.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.widget.Button;

public class Two extends Activity {

Button Button01;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);

    Button next = (Button) findViewById(R.id.Button01);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Gallery.class);
            startActivityForResult(myIntent, 0);
        }

    });
}
}

gallery.java

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;




public class Gallery extends FragmentActivity {
ViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);
    mPager = (ViewPager) findViewById(R.id.frame);
    mPager.setOffscreenPageLimit(1);
    mPager.setAdapter(new EndLessAdapter(this, mImageArray));

    // Look up the AdView as a resource and load a request.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
}

private int[] mImageArray = {R.drawable.y1,};


}

In the setContentView method of your Two Activity replace R.layout.gallery with R.layout.activity_main and replace view.getCotext() with two.this
Intent myIntent = new Intent( Two.this , Gallery.class);

The button you used is declared in R.layout.activity_main not R.layout.gallery . So when you use in Activity Tow , it is not invoked rightly...

Try to replace this code :

Your given wrong xml reference to yor Two Activity is activity_main instead of gallery.

public class Two extends Activity {

Button Button01;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button next = (Button) findViewById(R.id.Button01);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Gallery.class);
            startActivityForResult(myIntent, 0);
        }

    });
}
}

add this to the android manifest under the first activity

<activity
        android:name=".gallery" >
</activity>

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