需要帮助找到错误的位置。 (不幸的是,“App”已停止)

时间:2018-05-18 09:22:13

标签: java android debugging android-logcat

我还在学习如何使用Android Studio编写Android应用程序代码。这是我试图编写的应用程序有多个活动。每当我运行它时,只要我在主活动页面的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>

我不确定这是不是正确的地方,或者我是否正确地做到了这一点。我在电脑前独自在关丹学习,我身边没有人请求帮助。

2 个答案:

答案 0 :(得分:1)

您在尝试获取视图后设置了内容视图,因此它会崩溃,因为在您执行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);
}

答案 1 :(得分:0)

您的应用程序崩溃是因为您在设置内容视图之前尝试查找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);                  
    }

希望它会对你有所帮助!!