我正在嘗試在兩個活動之間導航,但應用程序崩潰並且要求強行關閉。活動之間的導航沒有發生
這裏是我的代碼: 1日活動
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 - 活性2的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
你看過崩潰的堆棧跟蹤嗎?如果您正在使用Eclipse,請切換到查看LogCat並查找崩潰時噴出的堆棧跟蹤。如果您無法理解堆棧跟蹤,請將其張貼到此處供我們查看。 – glorifiedHacker
我收到NullPointerException和堆棧跟蹤說 異常處理異步線程隊列 異常處理異步線程隊列 org.eclipse.jdt.internal.debug.core.model.JDIObjectValue無法轉換爲org.eclipse.jdt.debug。 core.IJavaArray – captgeek029
如果下面的兩個答案不支持您的解決方案(順便說一句,從HelloWorldActivity.this獲取上下文是正確的),但是您可以從LogCat中獲取儘可能多的堆棧跟蹤信息。 NullPointerException的來源可能被埋在堆棧跟蹤的某處。 – glorifiedHacker