如何解决“单击按钮时应用崩溃”的问题?

时间:2020-05-11 10:11:06

标签: java firebase android-studio

每当我按下第二个按钮(fireb),我的应用就会崩溃。但是,第一个按钮(单击)可以正常工作。

public class MainActivity extends AppCompatActivity {

    Button click;

    public  static TextView data;

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

        click = (Button) findViewById(R.id.button);
        data = (TextView) findViewById(R.id.fetcheddata);

        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fetchData process = new fetchData();

                process.execute();

            }

        });

        Button fireb =(Button) findViewById(R.id.fire);

        fireb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Member go = new Member();

                go.execute();
                DatabaseReference reff;
                reff= FirebaseDatabase.getInstance().getReference().child("Member");
                reff.push().setValue(go);
                Toast.makeText(MainActivity.this,"Data Inserted Sucessfully",Toast.LENGTH_LONG).show();
            }
        });

实际上,我想使用对象“ go”执行类“ Member”,然后要将“ go”中包含的数据发布到Firebase数据库。

这是崩溃日志。...

--------- beginning of crash
05-11 14:35:51.301 3457-3457/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 3457
    com.google.firebase.database.DatabaseException: Invalid key: . Keys must not contain '/', '.', '#', '$', '[', or ']'
        at com.google.firebase.database.core.utilities.Validation.validateWritableKey(com.google.firebase:firebase-database@@19.3.0:120)
        at com.google.firebase.database.core.utilities.Validation.validateWritableObject(com.google.firebase:firebase-database@@19.3.0:103)
        at com.google.firebase.database.DatabaseReference.setValueInternal(com.google.firebase:firebase-database@@19.3.0:283)
        at com.google.firebase.database.DatabaseReference.setValue(com.google.firebase:firebase-database@@19.3.0:159)
        at com.example.myapplication.MainActivity$2.onClick(MainActivity.java:51)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-11 14:36:01.830 3457-3472/? I/art: Background partial concurrent mark sweep GC freed 17161(834KB) AllocSpace objects, 9(611KB) LOS objects, 39% free, 3MB/5MB, paused 545us total 172.404ms
05-11 14:36:05.223 3457-3457/? I/Process: Sending signal. PID: 3457 SIG: 9

这是成员类。

public class Member extends AsyncTask<Void,Void,Void> {
    String mdata = "";
    String ad_id = "";
    String price = "";
    String username = "";

    @Override
    protected Void doInBackground(Void... voids) {

        try {
                   .
                   . 
                   .
            JSONObject JO1 = new JSONObject(mdata);
            JSONObject JO2 = JO1.getJSONObject("data");
            JSONArray JA = JO2.getJSONArray("ad_list");
            JSONObject JO3 = JA.getJSONObject(0);
            JSONObject JO4 = JO3.getJSONObject("data");
            JSONObject JO5 = JO4.getJSONObject("profile");

            username = (JO5.getString("username")).trim();
            ad_id = JO4.getString("ad_id").trim();
            price = JO4.getString("temp_price").trim();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return null;
    }

}

可能很简单,但我正在开发我的第一个应用程序:)

2 个答案:

答案 0 :(得分:0)

此问题与日志中显示的Firebase有关。

答案 1 :(得分:0)

原因是您使用以下方法设置了值:

无效密钥:。键不得包含'/'、'.'、'#'、'$','['或']

但是在查看您的代码后,有些事情我无法理解。

            go.execute();
            DatabaseReference reff;
            reff= FirebaseDatabase.getInstance().getReference().child("Member");
            reff.push().setValue(go);

您执行AsyncTask,但您不听结果。