没有在活动之间进行导航

时间:2011-08-11 15:55:40

标签: android

我正在尝试在两个活动之间导航,但应用程序崩溃并请求强制关闭。

这是我的代码: 第一项活动

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */

//public boolean returnFlag = false;
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button YesButton = (Button) findViewById(R.id.button1);
    YesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
    Intent myIntent = new Intent(view.getContext(),Activity2.class);
            startActivity(myIntent);
            finish();
          }
    });


}

第二项活动:

public class Activity2 extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.answerspage);


    Button butTryAgain = (Button) findViewById(R.id.id_tryagain);
    butTryAgain.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
    Intent myIntent = new Intent(view.getContext(), HelloWorldActivity.class);
            startActivity(myIntent);
            finish();
        }
    });
}

}

main.xml - HelloWorldActivity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="wrap_content"             

android:layout_height="wrap_content"android:textAppearance="?

android:attr/textAppearanceLarge" android:id="@+id/textView1"         `android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="27dp" android:text="Welcome"></TextView>`
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" `android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView2" android:text="Is your question ready??" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="29dp"></TextView>`
<Button android:text="Yes" android:layout_width="wrap_content" `android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_alignLeft="@+id/textView2" android:layout_marginLeft="36dp"></Button>`
<Button android:text="No"  android:layout_width="wrap_content" 

android:layout_height="wrap_content" android:id="@+id/button2" `android:layout_below="@+id/textView2" android:layout_alignRight="@+id/textView2" android:layout_marginRight="38dp" android:layout_marginTop="54dp"></Button>`
</RelativeLayout>

answerspage.xml - Activity2的xml文件

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/FrameLayout1" `xmlns:android="http://schemas.android.com/apk/res/android"`
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button android:layout_width="wrap_content" 

android:layout_height="wrap_content" android:text="Try again" android:id="@+id/button1" `android:layout_alignBaseline="@+id/id_tryagain" android:layout_alignBottom="@+id/button2"` `android:layout_alignParentLeft="true" android:layout_marginLeft="68dp"></Button>`


       <Button android:layout_height="wrap_content" android:text="Quit" `android:id="@+id/button2" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/id_quit" android:layout_marginLeft="29dp" android:layout_marginTop="127dp" android:layout_width="wrap_content"></Button>`
        <TextView android:layout_width="wrap_content" `android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:text="TextView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="46dp" android:editable="true"></TextView>`
     </RelativeLayout>    

这是我的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.HelloWorld"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HelloWorldActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Activity2"></activity>
</application>
</manifest>


I have gone through http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/ to implement the same

2 个答案:

答案 0 :(得分:3)

在onClick方法中,不应使用view.getContext()来启动intent。 相反,使用:

Intent myIntent = new Intent(HelloWorldActivity.this,Activity2.class);

表示HelloWorldActivity和

Intent myIntent = new Intent(Activity2.this, HelloWorldActivity.class);

for Activity2

答案 1 :(得分:1)

您正在使用的上下文是错误的。

尝试这样做

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */

private Context context = null;

//public boolean returnFlag = false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set the context here
    context = getApplicationContext();

    Button YesButton = (Button) findViewById(R.id.button1);
    YesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(context ,Activity2.class);
            startActivity(myIntent);
            finish();
        }
    });


}

希望这有帮助。