2014-10-17 88 views
0

我輸入的文本這是我的輸入編輯文本類如何顯示來自其他類

package com.example.websocketclient; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

EditText name; 
Button enter; 

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

     enter.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       name = (EditText) findViewById(R.id.editTextdname); 
       String dname = name.getText().toString(); 
       sendname(dname);     
      } 
     }); 

    } 

    public void sendname(String dname) { 
     Intent i = new Intent(this,Chatroom.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("myname", dname); 
     i.putExtras(bundle); 
     startActivity(i); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

這是我顯示的EditText TextView的,以一流的

package com.example.websocketclient; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.TextView; 

public class Chatroom extends Activity { 

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

     uname = (TextView) findViewById(R.id.textViewmynam); 

     //kuha 
     Bundle bundle = getIntent().getExtras(); 
     String urname = bundle.getString("myname"); 
     uname.setText(urname); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.chatroom, menu); 
     return true; 
    } 

} 

當我運行它..它說不幸的是,那麼它會關閉.. 我想從 - > EditText傳遞數據到我的其他類到我的顯示類 - > TextView。

+0

你只是忘了在'onCreate()'方法中'初始化'''輸入''Button'。 – Piyush 2014-10-17 05:42:47

回答

0

它說,遺憾的是,那麼它會關閉..

因爲你不打電話setOnClickListener之前初始化enter Button對象。做到這一點的:

setContentView(R.layout.activity_main); 
// initilize Button here 
enter= (Button) findViewById(R.id.BUTTON_ID_IN_XML); 
1

隨着ρяσѕρєяķ答案,傳遞的價值你,因爲你是隻傳遞一個字符串,你可以使用:

intent.putExtra("value","key"); 

,並同時獲得,

String value= getIntent.getStringExtra("key","default_value"); 
相關問題