开发者

Navigating between activities is not happening

I am trying to navigate between two activities but the application crashes and requests for a force close.

Here is my code: 1st Activity

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


}

2nd Activity:

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 - of HelloWorldA开发者_如何学JAVActivity

<?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 - xml file of Activity2

        <?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>    

This is my 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


In onClick method, you shouldn't use view.getContext() to start the intent. Instead, use :

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

for HelloWorldActivity and

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

for Activity2


The context you're using is wrong.

try doing this

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


}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜