2016-12-16 50 views
-3

我有一个edittext,您可以在其中输入您的姓名,以便在新活动中使用该名称。但是当我进入下一页时,我遇到了一个错误。如何在多个其他活动中使用Edittext

用的EditText活动的代码:

public class aanmeldbalie extends AppCompatActivity { 

    EditText SendValue; 
    Button SendEditTextValue; 
    Intent intent; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_aanmeldbalie); 

     SendEditTextValue = (Button)findViewById(R.id.buttonAanmeldbalie); 
     SendValue = (EditText)findViewById(R.id.kandidaatsnaam); 

     SendEditTextValue.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       intent = new Intent(getApplicationContext(),wachtkamer.class); 
       intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString()); 
       startActivity(intent); 

      } 
     }); 

    } 

    public void sendMessage(View view) { 
     Intent wachtkamer = new Intent(this, wachtkamer.class); 
     startActivity(wachtkamer); 
    } 

在这里我们要使用的EditText的活动代码:

TextView receive; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_dokter); 

     receive = (TextView)findViewById(R.id.kandidaatsnaam); 

     receive.setText(getIntent().getStringExtra("EdiTtEXTvALUE")); 
    } 

    public void sendMessage(View view) { 
     Intent uitleg_zuster = new Intent(this, uitleg_zuster.class); 
     startActivity(uitleg_zuster); 
    } 

的EditText上的XML代码:

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Naam" 
    android:layout_below="@id/tekstvak1" 
    android:paddingLeft="16dp" 
    android:background="#B3FFFFFF" 
    android:padding="8dp" 
    android:id="@+id/kandidaatsnaam" 
    /> 
+0

使用SharedPreference获取值随时随地在应用程序中 –

+0

必须确保您不是从xml和java文件中为单个按钮启动两次活动 – Piyush

+0

如果您使用的是“android:onClick:sendMessage”,那么为什么你使用点击监听器的按钮,你的sendMessage()方法被调用,你没有传递任何意图,因此你得到空指针异常 – Mrinmoy

回答

0
Remove the below code:- 

** 

    SendEditTextValue.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       intent = new Intent(getApplicationContext(),wachtkamer.class); 
       intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString()); 
       startActivity(intent); 
      } 
     }); 

** 
And add the below make sendMessage() like this: 


public void sendMessage(View view) { 
    Intent uitleg_zuster = new Intent(this, uitleg_zuster.class); 
intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString()); 
    startActivity(uitleg_zuster); 
} 

Make sure u have android:onClick="sendMessage" in xml file 

<Button 
    android:text="send" 
    android:onClick="sendMessage"/> 
0

使oncli CK这样并且不需要发送消息功能。

SendEditTextValue.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      intent = new Intent(aanmeldbalie.this,wachtkamer.class); 
      intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString()); 
      startActivity(intent); 

     } 
    }); 

而且在把这段代码你的第二个活动

receive.setText(getIntent().getStringExtra("EdiTtEXTvALUE")); 

内尝试捕捉或检查空值从空指针异常来阻止。

相关问题