非常简单的android应用程序崩溃,因为View为null

时间:2018-08-28 23:49:06

标签: android

我正在学习使用Android Studio为Android编写应用程序。我正在遵循一个教程来创建一个具有纯文本和按钮的简单活动(MainActivity)。输入一些文本并单击按钮应仅启动另一个活动(DisplayMessageActivity),该活动显示MainActivity中的文本。我正在按照教程中的说明完全键入所有内容,但是由于View为null,因此应用程序在findViewByID崩溃。如果有人可以提供帮助,我将不胜感激。这是这两个活动的.xml和.java文件。我评论了它崩溃的地方。

public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendMessage(View v)
    {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        setContentView(R.layout.activity_main);
        EditText editText = (EditText) v.findViewById(R.id.editText9);
        // IT CRASHES HERE BECAUSE v IS NULL
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="25dp">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="sendMessage"
        android:text="@string/button_send"
        app:layout_constraintBaseline_toBaselineOf="@+id/editText9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/editText9" />

    <EditText
        android:id="@+id/editText9"
        android:layout_width="143dp"
        android:layout_height="41dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="@string/edit_message"
        android:inputType="text"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


public class DisplayMessageActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView = findViewById(R.id.textView);
        textView.setText(message);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayMessageActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="28dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="30dp" />
</android.support.constraint.ConstraintLayout>

5 个答案:

答案 0 :(得分:0)

尝试将此代码放在此行之后

String message = editText.getText().toString();


if (message  != null && !message.getText().toString().isEmpty() )
{   
message  = message.getText().toString();
}

或者您可以尝试捕获v

          public void sendmessage(View v) {

          String intent = textPhoneNo.getText().toString();
          setContentView(R.layout.activity_main);
          EditText editText = (EditText) v.findViewById(R.id.editText9);
          try {
            String message = editText.getText().toString();
            message message = message.getDefault();


          } catch (Exception v) {
            message.makeText(getApplicationContext(),
                "Ops !! something went wrong",
                message.LENGTH_LONG).show();
            e.printStackTrace();
          }

会编译吗?

答案 1 :(得分:0)

更改sendMessage函数:

public void sendMessage(View v)
    {
        Intent intent = new Intent(this, DisplayMessageActivity.class);

        //layout for activity was set at onCreate
        //setContentView(R.layout.activity_main);

        // you should find edittext in activity layout but you find in view v
        //EditText editText = (EditText) v.findViewById(R.id.editText9);
        EditText editText = (EditText) findViewById(R.id.editText9);

        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

答案 2 :(得分:0)

问题是您在打电话

EditText editText = (EditText) v.findViewById(R.id.editText9);

代替

EditText editText = (EditText) findViewById(R.id.editText9);

单击按钮并调用该函数时,传入的View就是按钮的功能。调用findViewById会查找一个子视图,该视图在父活动中而不是按钮中,因此editText为空。

此外,请勿在{{1​​}}例程中调用setContentView,您已经在sendMessage中调用了

答案 3 :(得分:0)

将发送消息更改为如下

 public void sendMessage(View v)
{
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText9);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
  1. 已在onCreate()中删除了行setContentView(R.layout.activity_main)

2。由于edittext存在于activity_main.xml中,因此您将EditText editText = (EditText) v.findViewById(R.id.editText9);更改为EditText editText = (EditText) findViewById(R.id.editText9);

答案 4 :(得分:0)

将MainActivity更改为此。

public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void sendMessage(View v)
{
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    setContentView(R.layout.activity_main);
    EditText editText = (EditText) v.findViewById(R.id.editText9);
    // IT CRASHES HERE BECAUSE v IS NULL
    String message = editText.getText().toString();
    intent.putExtra("message", message);
    startActivity(intent);
}
}
  

将DisplayMessageActivity更改为此。

    public class DisplayMessageActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        Intent intent = getIntent();
        String message = intent.getStringExtra("message");
        TextView textView = findViewById(R.id.textView);
        textView.setText(message);
    }
}