0

I don't know what's going on here. I have tried surfing internet it. When I run it my app is crashing. The prblem still exists when implement click listner. I need some expert hand here... Is there any suggestion I could use?

public class Update extends AppCompatActivity {

    String tempobj;
    EditText name;
    Button s;

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

        name = (EditText) findViewById(R.id.rightupname);

        RadioGroup rg = (RadioGroup) findViewById(R.id.radservgrp);
        String radiovalue = ((RadioButton) findViewById(rg.getCheckedRadioButtonId())).getText().toString();
        Toast.makeText(getApplicationContext(),
            radiovalue, Toast.LENGTH_LONG).show();


        ParseQuery<ParseObject> query = ParseQuery.getQuery("serviceop");
        query.whereEqualTo("no", "6382551203");
        query.findInBackground(new FindCallback<ParseObject>() {

            public void done(List<ParseObject> objects, ParseException e) {

                if (e == null) {

                    if (objects.size() > 0) {

                        for (ParseObject object : objects) {

                            String one = object.getString("name");
                            name.setText(one);
                        }

                    } else {

                        Log.d("no data", "left or right");
                    }
                } else {
                    Log.d("awserror", "left or right");
                }
            }
        });
    }


    public void onSave(View view) {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("serviceop");
        query.getInBackground(makeobj("6382551203"), new GetCallback<ParseObject>() {
            public void done(ParseObject Grillen, ParseException eg) {
                if (eg == null) {

                    Grillen.put("name", name.getText().toString());
                    Grillen.saveInBackground(new SaveCallback() {
                        public void done(ParseException e) {
                            if (e == null) {

                                Log.d("MyApp", "Successfully saved!");
                            } else {

                                e.printStackTrace();
                            }
                        }
                    });
                } else {

                    eg.printStackTrace();
                }
            }
        });

        Log.d("sdas","sda");
    }


    public String makeobj(String nos) {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("serviceop");
        query.whereEqualTo("no",nos);
        query.findInBackground(new FindCallback<ParseObject>() {

            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {

                    if (objects.size() > 0) {

                        for (ParseObject object : objects) {

                            tempobj = object.getObjectId();
                        }

                    } else {

                        Log.d("no data","left or right");

                    }
                } else {
                    Log.d("awserror","left or right");
                }
            }
        });

        return tempobj;

    }
}

My log cat:

08-27 19:55:09.354 5672-5672/com.parse.starter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.parse.starter, PID: 5672
java.lang.IllegalStateException: Could not find method onSave(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'saveactive'
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

My xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical" 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=".Update">


<LinearLayout
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/sname1"
        android:text="Name"
        android:layout_weight="3"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <EditText
        android:id="@+id/rightupname"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>


<LinearLayout
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/sname2"
        android:text="Service type"
        android:layout_weight="3"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/radservgrp"
        android:layout_width="wrap_content"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_weight="3"
      >

        <RadioButton
            android:id="@+id/radcar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Car" />

        <RadioButton
            android:id="@+id/radbike"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No" />

    </RadioGroup>

</LinearLayout>


<LinearLayout
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/sname3"
        android:text="Fuel backup"
        android:layout_weight="3"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/radfuelgrp"
        android:orientation="horizontal"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radfuelyes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yes"
            android:checked="true" />

        <RadioButton
            android:id="@+id/radfuelno"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No" />

    </RadioGroup>

</LinearLayout>


<LinearLayout
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/sname4"
        android:text="Location"
        android:layout_weight="3"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:text="Change work location"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


<LinearLayout
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/sname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Pro membership"
        android:textStyle="bold" />

    <Button
        android:layout_marginTop="10dp"
        android:id="@+id/activepro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="active pro membership" />

</LinearLayout>


<Button
    android:id="@+id/saveactive"
    android:text="save"
    android:onClick="onSave"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>
YakovL
  • 7,557
  • 12
  • 62
  • 102
stack subash
  • 61
  • 1
  • 3

1 Answers1

0

I think that your Activity needs the onClick() method. Try adding this one:

public void onSave(View view) {
    //your code goes here
}
  • no it does't. This is the case in which you decide that the Activity implements OnClickListener – TibiG Aug 27 '18 at 14:51
  • The `Button` in your `XML` layout file contains the `android:onClick="onSave"` attribute. The `LayoutInflater` will look for the `onSave` method in the `Context` instance. If it doesn't find it, the app will crash. You should either add the `onSave()` method to your `Activity` as demonstrated in this post or remove the attribute to prevent the crashes. – Wojciech Januszek Aug 27 '18 at 14:55
  • You've changed your answer to write onSave instead of onClick :). He has that method already check his snippet. – TibiG Aug 27 '18 at 14:57
  • @TibiG, yes I made a mistake when typing :) I guess that the app instance which crashed because of this exception hadn't this method implemented or `activity_update.xml` is not the file presented in the question as you've suggested in the comment. – Wojciech Januszek Aug 27 '18 at 15:00
  • yeah i have done what you have all said still doesnt work!!! – stack subash Aug 27 '18 at 15:44