2012-12-01 98 views
2

我有2個XML,這個名字是formOne.xml和formTwo.xmlfindViewById其他佈局

formOne.xml是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp"> 

    <EditText 
     android:id="@+id/editTextName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

    </LinearLayout> 

和fomTwo.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp"> 

    <Button 
      android:id="@+id/buttonSave" 
      android:layout_width="120dp" 
      android:layout_height="wrap_content" 
      android:text="@string/btSave" /> 

    </LinearLayout> 

和我有兩個活動,名稱是FormOne.java和FormTwo.java

FormOne.java:

public class FormOne extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.formOne); 
      EditText etName = (EditText) findViewById(R.id.editTextName); 
} 

}

和FormTwo.java:

public class FormTwo extends Activity implements OnClickListener { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.formTwo); 

    EditText etName = (EditText) findViewById(R.id.editTextName); 

    Button btSave = (Button) findViewById(R.id.buttonSave); 
    btSimpan.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      if (etName.getText().length() != 0) { 
       Toast.makeText(FormTwo.this, "Name is : "+etName.getText().toString(), Toast.LENGTH_SHORT).show(); 
      } 
      else { 
       Toast.makeText(FormTwo.this, "Input the name please", Toast.LENGTH_SHORT).show(); 
      } 
      // TODO Auto-generated method stub 
     } 
    }); 
} 

我想從editTextName趕上價值,價值可以FormTwo.java顯示與吐司。

但我的應用程序的力量關閉, 我怎麼能得到值editTextName,而不用捆綁傳遞數據。因爲在FormOne.java中沒有任何按鈕發送值editTextName。

由於提前

+1

那麼你怎樣才能打開FormTwo活動? –

+0

我使TabActivity,與2選項卡。第一個選項卡是FormOne.java,第二個選項卡是FormTwo.java – pwcahyo

+0

然後變量etName Form2活動從哪裏? Form2中沒有任何變量引用etName。 – pwcahyo

回答

0

保存edittext價值爲sharedpreferencesontextchangelistener()事件的EditText的,並從sharedpreferences數據到Form2的活動。

+0

我寫etName =(EditText)findViewById(R.id.editTextName);在setContent之後的formOne.java文件中,但保留關閉 – pwcahyo

+0

@insomniart用最後一個代碼和錯誤logcat更新您的問題。 –

+0

最後一個代碼,被編輯, 和日誌貓這樣 線程ID = 1:螺紋,未捕獲的異常退出(組= 0x40015560) 顯示java.lang.NullPointerException – pwcahyo

0

第一個活動必須啓動第二個活動,將String對象作爲參數傳遞。否則,你如何確保第一個活動真正開始,用戶在文本字段中鍵入了什麼?

如果你真的不想按需開始第二個活動,你唯一的選擇是使用一個單獨的存儲庫,如SQLiteDatabase,共享首選項,文件或存儲在互聯網上的東西,並使第二個選擇一個值異步從該存儲庫,最終提供一個默認值,以解決用戶沒有明確保存任何內容時的情況。

很明顯,第一個活動必須在要求時寫入這個存儲庫。但是這種設計對我來說可能看起來很麻煩(即通過一個persisten存儲讀/寫來傳遞字符串)

0

您試圖獲得第二個活動中第一個活動中存在的編輯文本的引用。所以它可能會拋出空指針異常並導致崩潰。您不能在第二個活動的第一個活動中參考編輯文本。您將獲得您正在使用setcontentview()爲該活動設置的視圖的參考。

0

首先你的應用程序會崩潰,因爲你沒有在你的xml中爲第二個活動添加EditText,並且你在第二個活動的onCreate()方法中聲明它。

其次,如果您想在不使用意圖的情況下在您的活動之間檢索字符串。putExtra( 「」);方法你可以做如下:

1,你可以創建獨立的類是這樣的:

public class Myclass{ 
    private String myString; 
    public void setString(String str){ 
    myString=str; 
    } 
    public String getString(){ 
    return myString; 
    } 
} 

2 - 和你的活動1,你可以通過

​​

3將值分配給該字符串 - 你可以在任何你想要的地方打這個字符串:

String myString=MyClass.getString():