-3

I'm still learning how to code Android apps using Android Studio. This is the app I'm trying to write to practice having more than one activity. Everytime I run it it says "Unfortunately Multiactivity has stopped" whenever I type something in the EditText in the main activity page and click the button.

Perhaps you guys can help me in finding the error?

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);
}
}

And then the logcat, which suggests the problem lies in the onClick but that's all I could deduce so far,

 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"

Pasting the whole logcat here seems to mess up the Code format on this website, apologies.

This is the strings.xml file if it helps?

<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>

I'm not sure if this is the right place, or if I'm doing this properly. I'm studying alone in Kuantan in front of my computer and I have no one around me to ask for help.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Dudu Krem
  • 67
  • 3
  • 11

2 Answers2

1

You are setting the content view after trying to get views, so it will crash because in the point you're doing findViewById, the views are null.

Before call findViewById, you must call setContentView(), so setContentView() should be always the firs instruction after super.onCreate(). After setting the content view you can start finding views by ID.

Edit your onCreate() method setting setContentView() as follows:

@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);
}
Marc Estrada
  • 1,657
  • 10
  • 20
0

You app is crashing because you trying to find id view before set content view.

Replace this:

@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);
}      

To:

 @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);                  
    }

Hope it will help you!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49