我在這裏尋求一些幫助,我的代碼正面臨着一條死衚衕。我試圖使用Intent將screen1.java中的值傳遞給screen2.java。傳遞價值是好的,我設法通過它;然而,當我檢查使用if語句程序崩潰。下面是我的文件,plzzzzzzzzzzz幫助調用同一類中的不同圖層
screen1.java
package test.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class screen1 extends Activity
{
static String strKey = "Hello";
static final String strValue = "Hello";
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.screen1);
//** button A
Button A = (Button) findViewById(R.id.btnClickA);
A.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(screen1.this, screen2.class);
strKey = "NAME";
i.setClassName("packageName", "packageName.IntentClass");
String term = "Hello";
i.putExtra("packageName.term", term);
//i.putExtra(strKey, strValue);
startActivity(i);
}
});
//**
//** button B
Button B = (Button) findViewById(R.id.btnClickB);
B.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(screen1.this, screen3.class);
startActivity(i);
}
});
//**
}
}
screen2.java
package test.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class screen2 extends Activity
{
public void onCreate(Bundle icicle)
{
Bundle extras = getIntent().getExtras();
String term = extras.getString("packageName.term");
System.out.println("--- Name is -->"+ term);
if(term.equalsIgnoreCase("Hello") || term.equalsIgnoreCase("Name")){
super.onCreate(icicle);
setContentView(R.layout.screen3);
Button b = (Button) findViewById(R.id.btnClick3);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
} else {
super.onCreate(icicle);
setContentView(R.layout.screen2);
Button b = (Button) findViewById(R.id.btnClick2);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
}
// DOES NOT WORK !!!!!!!!!
System.out.println("--- Name is -->"+ term);
}
}
佈局: screen1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the first Screen"
/>
<Button
android:id ="@+id/btnClickA"
android:background="@drawable/sleep0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Screen 2"
/>
<Button
android:id ="@+id/btnClickB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Screen 3"
/>
<ImageView android:id="@+id/icon" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:src="@drawable/icon"
android:layout_gravity="center"/>
</LinearLayout>
screen2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in Screen 2"
/>
<Button
android:id ="@+id/btnClick2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
/>
</LinearLayout>
screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- ScrollView: will allow the layout to be scroll Up/Down and Left/right -->
<ScrollView
android:id="@+id/widget54"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in Screen 3"
/>
<Button
android:id ="@+id/btnClick3"
android:background="@drawable/sss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
/>
<ImageView android:id="@+id/sssq" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:src="@drawable/sssq"
android:layout_gravity="center" />
</LinearLayout>
</ScrollView>
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.android">
<application android:icon="@drawable/icon">
<activity android:name="screen1"
android:label="SCREEN 1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="screen2"
android:label="SCREEN 2">
</activity>
<activity android:name="screen3"
android:label="SCREEN 3">
</activity>
</application>
</manifest>
===== 該錯誤是由這些行的代碼中引起screen2.java:
if(term.equalsIgnoreCase("Hello") || term.equalsIgnoreCase("Name")){
super.onCreate(icicle);
setContentView(R.layout.screen3);
Button b = (Button) findViewById(R.id.btnClick3);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
}
else {
super.onCreate(icicle);
setContentView(R.layout.screen2);
Button b = (Button) findViewById(R.id.btnClick2);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
}
**注意,如果我擺脫了整個IF語句,並只與ELSE一起使用,程序工作正常。
所以你說我應該只是越過這條線「i.setClassName(」packageName「,」packageName.IntentClass「)」?是的,調用screen3可以正常工作;當我點擊主屏幕(screen1.java)中的按鈕B時,它將我發送到screen3。 – khalid 2010-05-20 04:41:07
非常感謝你,我只是評論說,線條和它的工作就像一個魅力。 – khalid 2010-05-20 04:55:45