firebase setvalue崩溃android studio

时间:2018-10-14 04:59:53

标签: android firebase

我上课

    public class Expense{
        public String date;
        public String item;
        public int price;
        public Expense(){}
        public Expense(String date,String item,int price){
            this.date = date;
            this.item = item;
            this.price = price;
        }

        public void setDate(String date) {
            this.date = date;
        }
        public void setItem(String item){
            this.item = item;
        }
        public void setPrice(int price) {
            this.price = price;
        }
        public int getPrice() {
            return price;
        }
        public String getDate() {
            return date;
        }
        public String getItem() {
            return item;
        }
     }

和一个功能

public void createByDB(String[] data,int[] price ,int size,int[] pick){
    //String []data looks like ={"apple","banana","candy"....}
    //int []price looks like ={18,20,25,....}
    //size = the size of int[] pick
    //int[]pick looks like = {0,1,4,...} ,the numbers in this array is randomly pick,and the number won't excess the size of data and price
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("details");
    Calendar c = Calendar.getInstance();
    String date = c.get(Calendar.YEAR)+"/"+c.get(Calendar.MONTH)+"/"+c.get(Calendar.DAY_OF_MONTH);
    for(int i=0;i<size;i++) {
        Expense expense = new Expense(date,data[pick[i]],price[pick[i]]);
        myRef.push().setValue(expense);
    }
}

我不知道为什么我调用createByDB()会使我的程序崩溃

我检查了Logcat并发现了错误消息

  

2018-10-14 12:21:13.832   2236-27737 / com.google.android.googlequicksearchbox:search   E / AudioRecord-JNI:创建AudioRecord实例时出错:初始化   检查失败,状态为-12。 2018-10-14 12:21:13.832   2236-27737 / com.google.android.googlequicksearchbox:search   E / android.media.AudioRecord:初始化本机时错误代码-20   AudioRecord对象。

我用Google搜索了它,帖子说我必须向我的AndroidManifest.xml添加RECORD_AUDIO权限 所以我把这行加到了清单中 this line

更新,这是我的AndroidManifest.xml AndroidManifest.xml

但是当我调用createdByDB()时,我的程序仍然崩溃

我不知道我在哪里做错了。有人可以给我什么建议吗?谢谢!

更新: 如果我编辑

    for(int i=0;i<size;i++) {
        Expense expense = new Expense(date,data[pick[i]],price[pick[i]]);
        myRef.push().setValue(expense);
    }

对此

        Expense expense = new Expense(date,data[1],price[1]);
        myRef.push().setValue(expense);

它有效,我已经尝试过

    for(int i=0;i<size;i++)System.out.println("pick:"+pick[i]);

检查我是否超出了数据量和价格,但答案是否定的

那么,如何修改程序以执行我想要的工作?

1 个答案:

答案 0 :(得分:0)

您需要添加此行 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 以及录制和保存音频。

这些行还不够Android 6.0及更高版本的设备。在进行任何与录制有关的事情之前,请为您的应用添加这种类型的权限请求。

    private void checkPermissions() {

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

    }

}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {

    switch (requestCode) {

        case 1:
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, 2);
            break;

        default:
            break;
    }
}