-1

I am making a simple app in which context view changes and then a toast message is displayed as input given by user. I don't understand why the app keeps on crashing when changing the context view.

Also Android studio gives this warning:

Method "Toaster" is missing in "FirstActivity" or has incorrect signature.

Here is my code:

activity_me_clicked.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/welcome"
        android:textSize="36sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:paddingLeft="7dp"
        android:paddingRight="7dp"
        android:text="@string/intro"
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="7dp"
                android:layout_marginEnd="7dp"
                android:layout_marginTop="10dp"
                android:ems="10"
                android:hint="Name"
                android:inputType="textPersonName"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/named"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:onClick="MainProcess"
                android:text="@string/done" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            tools:ignore="UselessLeaf"></LinearLayout>

    </LinearLayout>

</LinearLayout>

FirstActivity.Java:

package com.example.nautatvanavlakha.abcd;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class FirstActivity extends AppCompatActivity {
    public static String owner_string;

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

    public void MainProcess(View view) {

        final String TAG="DEBUG";
        Log.d(TAG,"At least process started");

        EditText owner = (EditText) findViewById(R.id.name);
        owner_string = owner.getText().toString();
        Log.d(TAG,"owner name stored");

        TextView textView = (TextView) findViewById(R.id.welcome);
        textView.setText("Hi " + owner_string + ".");
        Log.d(TAG,"owner name is set");

        setContentView(R.layout.activity_main_screen);
        Log.d(TAG,"content shown");
    }
}

activity_main_screen.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/welcome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/what_would_you_like_me_to_do_today"
        android:textSize="18sp" />

    <Button
        android:id="@+id/Cam"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:padding="0dp"
        android:paddingTop="15dp"
        android:text="@string/camera" />

    <Button
        android:id="@+id/Mus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:padding="0dp"
        android:paddingTop="15dp"
        android:text="@string/music" />

    <Button
        android:id="@+id/QR"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:padding="0dp"
        android:paddingTop="15dp"
        android:text="@string/scanQR" />


    <EditText
        android:id="@+id/toastText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/order"
        android:inputType="textPersonName" />


    <Button
        android:id="@+id/toaster"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:onClick="toaster"
        android:padding="0dp"
        android:paddingTop="15dp"
        android:text="@string/toast"
        android:layout_marginEnd="100dp"
        android:layout_gravity="center"/>

</LinearLayout>

MainScreen.java:

package com.example.nautatvanavlakha.abcd;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainScreen extends AppCompatActivity {

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

    public void toaster(View view){

        EditText toast = (EditText)findViewById(R.id.toastText);
        String final_toast = toast.getText().toString();
        Toast.makeText(getApplicationContext(), final_toast, Toast.LENGTH_SHORT).show();
    }

}

EDIT: As suggested, I moved the toaser function to FirstActivity.Java and deleted the MainScreen.java file as it becomes pointless to keep it. But the major problem is when I press the button (id named) the app keeps stopping.

EDIT2: I found that setContentView(R.layout.activity_main_screen) in FirstActivity.Java needs to be above this code

    TextView textView = (TextView) findViewById(R.id.welcome);
    textView.setText("Hi " + owner_string + ".");
    Log.d(TAG,"owner name is set");

so that Activity has access to all the layout components. Thanks solved :)

  • The code is full of bugs, It's a wrong way to change the content of your Activity by this way, Your MainScreen Activity will stay in dark and never be used. – maysara Jul 04 '17 at 23:40

2 Answers2

1

You replaced the content view in the first activity with a layout that include the onClick attribute, but you have no public void toaster(View view) method there.

So, either don't use setContentView a second time, or implement that method on both Activities.

The recommended way to replace the view is Fragments, by the way

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Setting a new content view is a perfectly acceptable way to replace it if you want a permanent replacement for the entire layout. Fragments should be used if you want to reuse the same control logic in multiple places, or want to embed it as a UI element in another xml file. He wouldn't be wrong to use Fragments here, but its hardly a requirement (and fragment replacements have such a host of issues that I hesitate to suggest using it anywhere these days). Although looking at what his code is actually doing I'm not sure what he really wants. – Gabe Sechan Jul 04 '17 at 23:34
  • Calling setContentView and expecting view references to remain has its problem as well, so I'd say it's a fair trade off – OneCricketeer Jul 04 '17 at 23:36
  • Oh if you set a new context view, all the references need to be nulled out. But that technique isn't bad, although his implementation is not good. – Gabe Sechan Jul 04 '17 at 23:37
0

This is a wrong way to change the contents of your Activity, The main problem here is that you have onClick attribute set to "toaster", And you don't have a function called "toaster" in your FirstActivity.

Beside that, The MainScreen Activity in your code will never be used.

So, Your problem is that you set the FirstActivity contents to "activity_main_screen.xml", And FirstActivity doesn't have "toaster" method in it, When you change the context, the Activity will try to find toaster method and the app will crash because the method doesn't exist in FirstActivity.

You can solve this problem by making a method called "toaster" inside FirstActivity, But This is a very bad practice, You can use Fragments instead, Or you can use Intent to move to another activity.

Some useful links about Fragments:

https://developer.android.com/guide/components/fragments.html

https://www.tutorialspoint.com/android/android_fragments.htm

https://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/

Why should I use fragment in Android?

maysara
  • 5,873
  • 2
  • 23
  • 34
  • I am new to android and did not know much about fragments. I will try to use this into my code but where could my code possibly go wrong. Any help –  Jul 05 '17 at 04:54