【问题标题】:Opening a new activity on button click在按钮单击时打开一个新活动
【发布时间】:2013-04-10 06:42:36
【问题描述】:

我查看了每个在活动之间切换的示例,我总是得到相同的结果。应用程序炸弹。

据我所知,如果您有一个填充布局内容的 java 类,那么为了切换到另一个布局,您必须“链接”到 java 文件,该文件反过来将打开 setContentView(R .layout.whatever);

当我尝试这样做时,就像我说我的应用程序爆炸一样。我的代码如下:-

来自 Java 类:-

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.activity_main);

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

    });

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);   




}

到java文件(ac2)

     public class ac2 extends Activity {

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

     }}

有人可以帮忙吗?

【问题讨论】:

  • 什么是错误日志。您是否在清单中声明了活动?
  • 使用 startActivity(myIntent);,你可以(应该)使用 getApplicationContext();而不是 view.getContext();还要确保你已经在 androidmanifest.xml 中声明了你的活动,比如
    logcat 错误日志会很有帮助

标签: android class android-intent android-activity


【解决方案1】:

尝试从该行中删除零:

startActivityForResult(myIntent, 0);

像这样,然后将其更改为:

startActivity(myIntent);

并改变这一行:

Intent myIntent = new Intent(view.getContext(), ac2.class);

到这里:

Intent myIntent = new Intent(firstActivityName.this, ac2.class);

因为您在这里获得的是按钮的上下文而不是活动的上下文。

【讨论】:

    【解决方案2】:

    解决方案很简单,基于 VSK 的回复(谢谢)稍作调整。

    所需的 ImageButton :-

        <Button 
        android:id="@+id/previous" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="click"
        android:onClick="move" />
    

    需要Java:-

        public void move(View v)
        {
        Intent myIntent = new Intent(yourclass.this, ac2.class);
        startActivityForResult(myIntent, 0);
        }
    

    VSK - 请注意 startActivityForResult 的 '0' 属性

    谢谢大家

    【讨论】:

      【解决方案3】:

      试试这个方法

      在 xml 文件中为您的按钮添加 onclick 功能 像这样

      <Button 
              android:id="@+id/previous" 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" 
              android:text="click"
              android:onClick="move" />
      

      在你的java文件中 让函数像这样移动

      public void move(View v)
      {
               Intent myIntent = new Intent(yourclass.this, ac2.class);
              startActivityForResult(myIntent,0);
       }
      

      【讨论】:

      • VSK - 尝试您的方法时,由于出现错误,我无法保存和编译应用程序 - Activity 类型中的方法 startActivityForResult(Intent, int) 不适用于参数(Intent )
      猜你喜欢
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 2019-04-12
      • 2015-05-01
      • 1970-01-01
      相关资源
      最近更新 更多