簡體   English   中英

按下按鈕時,應用程序關閉。 目標是使用EditText的輸入設置TextView

[英]App shuts down when button is pressed. Objective is to set TextView with the input from EditText

我必須編寫一個程序,當我按下按鈕時,我在EditText中編寫的內容會顯示在TextView中。 但是,當我按下按鈕時,該應用程序將關閉。 我要去哪里錯了? 我嘗試過按照許多類似問題的解決方案所說的方法來做,但是沒有用。 請幫忙。 PS:我對Android編程一無所知。 任何幫助,將不勝感激。

這是我的主要課程:

public class abc extends Activity implements View.OnClickListener {
    EditText editText;
    TextView textView;
    Button button;
    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_abc);
        EditText editText=(EditText)findViewById(R.id.ed_edit);
        TextView textView=(TextView)findViewById(R.id.text_tview);
        Button button=(Button)findViewById(R.id.btn_button);
        button.setOnClickListener(this);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_button:
                name= editText.getText().toString();
                textView.setText(name);
                break;
        }
    }
}

這是xml代碼:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <TextView
        android:text="ABD"
        android:id="@+id/text_tview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        />

    <Button android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="HERE"
        android:id="@+id/btn_button"
        />

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

</LinearLayout>

您正在使用findViewById()初始化局部變量。 更換:

EditText editText=(EditText)findViewById(R.id.ed_edit);
TextView textView=(TextView)findViewById(R.id.text_tview);
Button button=(Button)findViewById(R.id.btn_button);

與:

editText=(EditText)findViewById(R.id.ed_edit);
textView=(TextView)findViewById(R.id.text_tview);
button=(Button)findViewById(R.id.btn_button);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM