我需要使用runTransaction

时间:2017-08-27 08:10:29

标签: android firebase firebase-realtime-database

我正在从本地json文件加载我的问题,每当我按下下一个按钮时,我需要将firebase中孩子的值增加1。但是,当我按下一个按钮时,应用程序崩溃了。

logcat显示以下错误

onFatalError,来自引擎的处理错误(4)                                                  com.google.android.apps.gsa.shared.speech.b.g:从输入流中读取错误                                                      在com.google.android.apps.gsa.staticplugins.recognizer.j.a.a(SourceFile:28)                                                      在com.google.android.apps.gsa.staticplugins.recognizer.j.b.run(SourceFile:15)                                                      at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:457)                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:266)                                                      在com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:14)                                                      在com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4)                                                      在com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4)                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)                                                      at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636)                                                      在java.lang.Thread.run(Thread.java:764)                                                      在com.google.android.apps.gsa.shared.util.concurrent.a.ai.run(SourceFile:6)                                                   引起:com.google.android.apps.gsa.shared.exception.GsaIOException:错误代码:393238 |缓冲区溢出,没有可用空间。                                                      在com.google.android.apps.gsa.speech.audio.Tee.f(SourceFile:103)                                                      在com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)                                                      在java.io.InputStream.read(InputStream.java:101)                                                      在com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:18)                                                      在com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)                                                      at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:457)                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:266)                                                      在com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:14)                                                      在com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4)                                                      在com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4)                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)                                                      at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636)                                                      在java.lang.Thread.run(Thread.java:764)                                                      在com.google.android.apps.gsa.shared.util.concurrent.a.ai.run(SourceFile:6)

public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getAssets().open("Attributes.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}




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

    Firebase.setAndroidContext(this);
    myDatabase=new Firebase("firebase url");

    FeedbackText=(TextView)findViewById(R.id.feedback);
    comment=(EditText) findViewById(R.id.commentbox);
    comment.setVisibility(View.INVISIBLE);



    marks=new String[10];
    ques=new String[8];
    final TextView Description=(TextView)findViewById(R.id.AttributeDesc);
    final Button nextBtn=(Button)findViewById(R.id.navigate);;
    Progress= (ProgressBar)findViewById(R.id.progressBar2);
    ratingBar=(RatingBar)findViewById(R.id.ratingBar);

    //parsing Json
    try {
        obj = new JSONObject(loadJSONFromAsset());
        m_jArry = obj.getJSONArray("Attributes");
        jo_inside = m_jArry.getJSONObject(0);
         Description.setText(jo_inside.getString("Q"));
        } catch (JSONException e1) {
        e1.printStackTrace();
    }

//下一个按钮监听器

    nextBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (i < 10) {
                try {
                    m_jArry = obj.getJSONArray("Attributes");
                    jo_inside = m_jArry.getJSONObject(i++);
                    Description.setText(jo_inside.getString("Q"));
                    Progress.setProgress(i);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                teacher = myDatabase.child("teacher_one").child(ques[i]).child(marks[i]);
                teacher.runTransaction(new Transaction.Handler() {
                    @Override
                    public Transaction.Result doTransaction(final MutableData currentData) {
                        if (currentData.getValue() != null) {
                            currentData.setValue((Long) currentData.getValue() + 1.0);
                        }
                        return Transaction.success(currentData);

                    }

                    @Override
                    public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {

                    }
                });



            }//end_if

            if (i == 9) {
                nextBtn.setText("Finish");
                ratingBar.setRating(0F);
                FeedbackText.setText("----");

            }
        }



    });
//rating bar listener
    ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {

            int ch=(int) v;
            switch(ch)
            {
                case 1:
                    FeedbackText.setText("Satisfactory");
                    marks[i]="0";
                    comment.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    FeedbackText.setText("Good");
                    marks[i]="1";
                    comment.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    FeedbackText.setText("Very Good");
                    marks[i]="2";
                    comment.setVisibility(View.INVISIBLE);
                    break;
                case 4:
                    FeedbackText.setText("Excellent");
                    marks[i]="3";
                    comment.setVisibility(View.INVISIBLE);
                    break;

            }
        }
    });

}

0 个答案:

没有答案