从同一个类调用不同的层

时间:2010-05-20 03:39:59

标签: java android android-intent layer

我在这里寻求一些帮助我的代码,我正面临一条死路。我正在尝试使用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,程序运行正常。

2 个答案:

答案 0 :(得分:0)

如果要将信息传递给Intent构造函数,则不需要

i.setClassName(“packageName”,“packageName.IntentClass”)。顺便问一下你的Screen3电话工作正常吗?

答案 1 :(得分:0)

您传输的数据不正确,请尝试以下操作:

public void onClick(View arg0) {
                Intent i = new Intent(screen1.this, screen2.class);
                strKey = "NAME";
                //NO NEED FOR THIS, REMOVE -> i.setClassName("packageName", "packageName.IntentClass");
                String term = "Hello";
                //Add your string to a bundle:
                Bundle b = new Bundle();
                b.putString("packageName.term", term);
                //Then add the bundle to your intent
                i.putExtra(b);

                //i.putExtra(strKey, strValue);
                startActivity(i);
            }

你在screen2活动中检索它的方式很好,我只会检查term是否为null:

if(term != null && (term.equalsIgnoreCase("Hello") || term.equalsIgnoreCase("Name"))){

...