【问题标题】:Using Android button to open new class/activity使用 Android 按钮打开新的类/活动
【发布时间】:2014-08-28 04:24:55
【问题描述】:

我在我的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>

安卓清单

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

两个.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,};


}

【问题讨论】:

    标签: java android class android-studio


    【解决方案1】:

    在您的两个 Activity 的 setContentView 方法中,将 R.layout.gallery 替换为 R.layout.activity_main 并替换 view .getCotext()two.this
    Intent myIntent = new Intent(Two.this, Gallery.class);

    【讨论】:

    • 谢谢,我试过了,我仍然可以点击按钮,但没有任何反应,它不会带我进入其他 xml 活动
    • 您的代码正常。我不知道那里有什么问题
    【解决方案2】:

    您使用的按钮在R.layout.activity_main 中声明,而不是在R.layout.gallery 中。所以当你在ActivityTow中使用时,它并没有被正确调用...

    【讨论】:

      【解决方案3】:

      尝试替换此代码:

      您对您的两个活动的错误 xml 引用是 activity_main 而不是画廊。

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

      【讨论】:

      • 我更改了对 R.layout.activity_main 的引用,不幸的是它仍然只是单击但没有带我其他 xml 活动..
      【解决方案4】:

      将此添加到第一个活动下的 android 清单

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-07
        • 2015-04-26
        • 1970-01-01
        • 1970-01-01
        • 2017-12-17
        • 2023-03-28
        相关资源
        最近更新 更多