需要帮助找出错误所在。 (不幸的是,"App"已经停止)

Need help finding where the error is. (Unfortunately, "App" has stopped)

我仍在学习如何使用 Android Studio 编写 Android 应用程序代码。这是我正在尝试编写的用于练习拥有多个 activity 的应用程序。每次我 运行 它都会说 "Unfortunately Multiactivity has stopped" 每当我在主 activity 页面的 EditText 中键入内容并单击按钮时。

也许你们可以帮我找出错误?

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rinorapps.com.multiactivity">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="rinorapps.com.multiactivity.MainActivity"/>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SecondActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="rinorapps.com.multiactivity.MainActivity"/>


    </activity>
</application>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<TextView
    android:id="@+id/main_activity_title"
    android:layout_width="match_parent"
    android:layout_height="53dp"
    android:gravity="center"
    android:text="@string/main_activity_title_text"
    android:textSize="18sp" />

<Button
    android:id="@+id/main_activity_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/main_activity_button_text"
    android:onClick="main_activity_button_listener"/>

<EditText
    android:id="@+id/main_edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="70dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="@string/main_edit_text" />

mainactivity.java

package rinorapps.com.multiactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

public static final String EXTRA_MESSAGE =
        "rinorapps.com.multiactivity.extra.MESSAGE";

private Button main_activity_button;
private EditText main_edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    main_activity_button = findViewById(R.id.main_activity_button);
    main_edit = findViewById(R.id.main_edit);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void main_activity_button_listener(View view) {
    Intent intent = new Intent(this, SecondActivity.class);
    String message = main_edit.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
}

Second_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".SecondActivity">

<TextView
    android:id="@+id/second_activity_title"
    android:layout_width="match_parent"
    android:layout_height="53dp"
    android:gravity="center"
    android:text="@string/second_activity_title_text"
    android:textSize="18sp" />

<EditText
    android:id="@+id/second_edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="138dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="@string/second_edit_text"/>

</RelativeLayout>

SecondActivity.java

package rinorapps.com.multiactivity;

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

public class SecondActivity extends AppCompatActivity {

private EditText second_edit;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    second_edit = findViewById(R.id.second_edit);
    second_edit.setText(message);
}
}

然后是 logcat,这表明问题出在 onClick 上,但到目前为止我只能推断出这些,

 1881-1881/rinorapps.com.multiactivity 
 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: rinorapps.com.multiactivity, PID: 1881
 java.lang.IllegalStateException: Could not execute method for android:onClick"

在这里粘贴整个 logcat 似乎弄乱了本网站的代码格式,抱歉。

如果有帮助,这是strings.xml文件?

<resources>
<string name="app_name">Multiactivity</string>
<string name="main_activity_title_text">Main Activity</string>
<string name="main_activity_button_text">Second</string>
<string name="second_activity_title_text">Second Activity</string>
<string name="main_edit_text">Type here</string>
<string name="second_edit_text">Hello</string>
</resources>

我不确定这是不是正确的地方,或者我这样做是否正确。我在关丹一个人在电脑前学习,周围没有人可以寻求帮助。

您在尝试获取视图后设置内容视图,因此它会崩溃,因为在您执行 findViewById 时,视图为空。

在调用 findViewById 之前,您必须调用 setContentView(),因此 setContentView() 应该始终是 super.onCreate() 之后的第一条指令。设置内容视图后,您可以开始按 ID 查找视图。

编辑您的 onCreate() 方法设置 setContentView() 如下:

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

   main_activity_button = findViewById(R.id.main_activity_button);
   main_edit = findViewById(R.id.main_edit);
}

您的应用程序崩溃是因为您试图在设置内容视图之前查找 ID 视图。

替换为:

@Override
protected void onCreate(Bundle savedInstanceState) {
    main_activity_button = findViewById(R.id.main_activity_button);
    main_edit = findViewById(R.id.main_edit);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}      

收件人:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_activity_button = findViewById(R.id.main_activity_button);
        main_edit = findViewById(R.id.main_edit);                  
    }

希望对您有所帮助!!