简体   繁体   English

当按下按钮时,应用崩溃

[英]When the button is pressed the app is crashing

I am trying to run an app and when I try to tap the button the app crashes when it is supposed to save data for backup 我正在尝试运行一个应用程序,当我试图点击该按钮时,该应用程序崩溃了,该应用程序应该保存用于备份的数据

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.back4app.userregistrationexample, PID: 31963
java.lang.IllegalStateException: Could not find method onclick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btn_addbalance'
            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
            at android.view.View.performClick(View.java:6329)
            at android.view.View$PerformClick.run(View.java:25002)
            at android.os.Handler.handleCallback(Handler.java:809)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:166)
            at android.app.ActivityThread.main(ActivityThread.java:7555)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
        I/Process: Sending signal. PID: 31963 SIG: 9
        Application terminated.

Here in the balance I only put the code that I think is needed but if the whole class is needed I will edit it too 在天平上,我只放置了我认为需要的代码,但是如果需要整个类,我也将对其进行编辑
Balance 平衡

@Override
 public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.app_bar_balance);
                Parse.initialize(new Parse.Configuration.Builder(this)
                        .applicationId("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
                        .clientKey("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
                        .server("https://parseapi.back4app.com/").build()
                );
                LiveQueryClient.init("wss://" + "aaaa.back4app.io", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", true); // Example: 'wss://livequerytutorial.back4app.io'
                LiveQueryClient.connect();

                Balance = (EditText) findViewById(R.id.txt_editbalance);

                final Button addbalance = findViewById(R.id.btn_addbalance);
                addbalance.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Validating the log in data
                        boolean validationError = false;

                        StringBuilder validationErrorMessage = new StringBuilder("Please, insert ");
                        if (isEmpty(Balance)) {
                            validationError = true;
                            validationErrorMessage.append("an balance");
                        }
                        validationErrorMessage.append(".");

                        if (validationError) {
                            Toast.makeText(Balance.this, validationErrorMessage.toString(), Toast.LENGTH_LONG).show();
                            return;
                        }
                        //Setting up a progress dialog
                        final ProgressDialog dlg = new ProgressDialog(Balance.this);
                        dlg.setTitle("Please, wait a moment.");
                        dlg.setMessage("Adding Load...");
                        dlg.show();

                        ParseUser balance = new ParseUser();
                        balance.setUsername(Balance.getText().toString());
                        balance.signUpInBackground(new SignUpCallback() {
                            @Override
                            public void done(ParseException e) {
                                if (e == null) {
                                    dlg.dismiss();

                                } else {
                                    dlg.dismiss();
                                    ParseUser.logOut();
                                    Toast.makeText(Balance.this, e.getMessage(), Toast.LENGTH_LONG).show();
                                }
                            }
                        });

                    }
                });
            }

I tried to change the names switching it back and forth hoping that it might fix the problem app bar balance 我试图更改名称来回切换,希望它可以解决应用栏平衡问题

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

        <TextView
            android:id="@+id/txt_currentbal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="59dp"
            android:text="Current Balance" />

        <TextView
            android:id="@+id/txt_balance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="96dp"
            android:text="android:id" />

        <TextView
            android:id="@+id/txt_addbal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="185dp"
            android:text="Add Balance" />

        <EditText
            android:id="@+id/txt_editbalance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="230dp"
            android:ems="10"
            android:hint="Add Balance"
            android:inputType="number" />

        <Button
            android:id="@+id/btn_addbalance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="177dp"
            android:onClick="onclick"
            android:text="Add Balance" />
    </RelativeLayout>

Now I removed the android:onClick="onclick" and now it looks like this but when I run it again on my phone now it doesn't do anything and the run doesn't even respond when the button is pressed 现在我删除了android:onClick="onclick" ,现在看起来像这样,但是当我再次在手机上运行它时,它什么也没做,按下按钮时运行甚至都没有响应

You already set ClickListener from your from your code. 您已经从代码中设置了ClickListener So button onClick attribute not needed. 因此不需要按钮onClick属性。

Just remove this line from the Button. 只需从Button中删除此行即可。

android:onClick="onclick"

It will look like this 看起来像这样

<Button
    android:id="@+id/btn_addbalance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="177dp"
    android:text="Add Balance" />

This will solve your problem. 这样可以解决您的问题。

In your XML you use a button from the support library (widget.AppCompatButton), but in your code you're trying to cast it to a regular Button. 在XML中,您使用支持库中的按钮(widget.AppCompatButton),但是在代码中,您尝试将其转换为常规Button。 Either use the regular Button in your XML code, or use the AppCompatButton in the Java code. 在XML代码中使用常规Button,或者在Java代码中使用AppCompatButton。

Seeing that you are using the support library (you're using the AppCompatActivity), I'd advice you to change 'Button' to 'AppCompatButton' in your Java code. 看到您正在使用支持库(您正在使用AppCompatActivity),建议您在Java代码中将“按钮”更改为“ AppCompatButton”。

将onclick()方法放在xml的按钮视图中

If you want to use in XML onClick then use like this : 如果要在XML onClick中使用,请使用以下代码:

<Button
    android:id="@+id/btn_addbalance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="177dp"
    android:onClick="onClick"
    android:text="Add Balance" />

you have entered in wrong format 您输入的格式错误

do this : "onClick" 做到这一点: "onClick"

instead of : "onclick " 代替: "onclick

you can check in logcats it's shows also same thing in it, So change it : 您可以签入logcats,它也显示相同的内容,因此请更改它:

Could not find method onclick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btn_addbalance' 在ID为'btn_addbalance'的视图类android.support.v7.widget.AppCompatButton上定义的android:onClick属性的父级或祖先上下文中找不到方法onclick(View)

by the way there is no need to add attribute in xml if you are doing in java 顺便说一句,如果您使用的是Java ,则无需在xml中添加属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM