简体   繁体   中英

firebase setvalue crash android studio

I have class

    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;
        }
     }

and a function

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);
    }
}

I don't know why if I call createByDB() would crash my program

I checked my Logcat and found error message

2018-10-14 12:21:13.832 2236-27737/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. 2018-10-14 12:21:13.832 2236-27737/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.

I googled it , the posts say I must add RECORD_AUDIO permission to my AndroidManifest.xml so I added this line into my manifest this line

Update This is my AndroidManifest.xml AndroidManifest.xml

but my program still crash when I call createdByDB()

I don't know where I did it wrong. Can someone give me any advice? Thank you!

Update: If I edit

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

to this

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

it works , I have tried

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

to check whether I excess the size of data and price ,but the answer is no

so, how can I revise my program to do what I want ?

You need to add this line <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> as well to record and save audio.

Those lines are not enough Android 6.0 and higher devices. Please add this type of permission request for your application before do any recording related things.

    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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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