【问题标题】:How do I move to a new activity in android?如何移动到 android 中的新活动?
【发布时间】:2014-01-03 00:55:03
【问题描述】:

我看到了人们发布的一些解决方案,但我在我的代码中找不到缺陷。它在打开时崩溃。

这是我的主要活动。

public class MainActivity extends Activity 
{
    Button guy, girl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        guy = (Button) findViewById(R.id.bGuy);
        girl = (Button) findViewById(R.id.bGirl);

        guy.setOnClickListener(new View.OnClickListener() {
            public void onClick(View aView)
            {
                Intent toAnotherActivity = new Intent(aView.getContext(), ScreenGuy.class);
                startActivityForResult(toAnotherActivity, 0);
            }
        });
    }   
}   

还有它的 .xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/get_over"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:text="@string/get_over"
    android:textSize="20sp" />

<Button
    android:id="@+id/bGuy"
    android:layout_width="120dp"
    android:layout_height="40dp"
    android:layout_alignLeft="@+id/get_over"
    android:layout_below="@+id/get_over"
    android:layout_marginTop="66dp"
    android:text="@string/guy" 
    android:onClick="sendMessage"
     />

<Button
    android:id="@+id/bGirl"
    android:layout_width="120dp"
    android:layout_height="40dp"
    android:layout_alignBaseline="@+id/bGuy"
    android:layout_alignBottom="@+id/bGuy"
    android:layout_alignRight="@+id/get_over"
    android:text="@string/girl"
     />

我想在按下“Guy”按钮时移动到另一个屏幕。

【问题讨论】:

    标签: java android eclipse android-intent android-activity


    【解决方案1】:

    使用这个:

    Intent i = new Intent(FirstActivity.this, SecondActivity.class);
    startActivity(i);
    

    您传递的不是点击视图的上下文,而是应用程序的上下文。

    【讨论】:

    • 很高兴为您提供帮助。快乐编码:)
    【解决方案2】:

    改变这个:

    Intent toAnotherActivity = new Intent(aView.getContext(), ScreenGuy.class);
    startActivityForResult(toAnotherActivity, 0);
    

    到这里:

    Intent toAnotherActivity = new Intent(MainActivity.this, ScreenGuy.class);
    startActivity(toAnotherActivity);
    

    正如你所说,你不需要为结果开始活动。此外,您将 Intent 传递给了错误的上下文:aView.getContext()。将您的 class 上下文传递给它。

    【讨论】:

      【解决方案3】:
      Intent intent = new Intent(MainActivity.this, ScreenGuy.class);
      startActivity(intent);
      

      编辑:它是这样的,因为如果你这么说,它指的是 onclick 监听器类的上下文,而不是活动

      【讨论】:

      • 这不会编译。此代码需要在 OnClickListener 内,因此范围不同。 this 必须是 MainActivity.this。 +1,如果你改变它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多