[英]How do I move to a new activity in android?
I've seen some solutions that people have posted, but I can't find the flaw in my code. 我已经看到了人们发布的一些解决方案,但是我找不到代码中的缺陷。 It crashes on opening.
打开时崩溃。
Here's my Main activity. 这是我的主要活动。
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);
}
});
}
}
And the .xml for it 还有.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"
/>
I have another screen I want to move to on the button press of "Guy". 我有另一个要移至“ Guy”按钮上的屏幕。
Intent intent = new Intent(MainActivity.this, ScreenGuy.class);
startActivity(intent);
EDIT: its like this because if you just say this, it refers to the context of the onclick listener class, not the activity 编辑:之所以这样,是因为如果您只是这样说,它是指onclick侦听器类的上下文,而不是活动
Use this: 用这个:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
You do not pass the context of the clicked view, but the context of the application. 您没有传递单击视图的上下文,而是传递了应用程序的上下文。
Change this: 更改此:
Intent toAnotherActivity = new Intent(aView.getContext(), ScreenGuy.class);
startActivityForResult(toAnotherActivity, 0);
to this: 对此:
Intent toAnotherActivity = new Intent(MainActivity.this, ScreenGuy.class);
startActivity(toAnotherActivity);
For what you've said, you don't need to start the activity for result . 对于您所说的,您不需要为result开始活动。 Also, you were passing the
Intent
the wrong context: aView.getContext()
. 另外,您还向
Intent
传递了错误的上下文: aView.getContext()
。 Pass it your class context. 将其传递给您的课程上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.