簡體   English   中英

編碼新手,應用在啟動時不斷崩潰

[英]New to coding, app keeps crashing on start up

我試圖有一個文本字段和一個文本視圖,並將文本視圖文本設置為該字段上的任何內容。

這是我的代碼:

MainActivity.java

            import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.widget.EditText;
        import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView helloMessage = (TextView) findViewById(R.id.hello); //define textView to show the message

        EditText  nameField = (EditText) findViewById(R.id.Name); // Text field to get the name from

        String  getName = nameField.getText().toString(); // set a variable with the text field's value

        helloMessage.setText(getName); // set text on the hello textview to be the name.


    }



}

activity_main.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B388FF"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/Name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hello"/>
</LinearLayout>

在此先感謝您提供的任何幫助!

兄弟,您需要setContentView(R.layout.activity_main); 在找到其他視圖之前

您忘記將setContentView(R.layout.activity_main)添加到onCreate()

暫無
暫無

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

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