使用Android按钮打开新的课程/活动

时间:2014-08-28 04:24:55

标签: java android class android-studio

我在activity_main上创建了一个文本按钮,点击后我想打开一个名为gallery的新xml。我为此创建了一个xml和类。当我点击按钮时,它不会带我到任何地方。

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


}

4 个答案:

答案 0 :(得分:0)

在“两个活动”的 setContentView 方法中,将 R.layout.gallery 替换为 R.layout.activity_main 并替换视图.getCotext() two.this
Intent myIntent = new Intent( Two.this ,Gallery.class);

答案 1 :(得分:0)

您使用的按钮在R.layout.activity_main而非R.layout.gallery中声明。因此,当您在活动Tow中使用时,它不会被正确调用...

答案 2 :(得分:0)

尝试替换此代码:

您给出的错误的xml对yor的引用是Activity_main而不是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);
        }

    });
}
}

答案 3 :(得分:0)

将此添加到第一个活动

下的 android清单
<activity
        android:name=".gallery" >
</activity>