java.lang.ExceptionInInitializerError-Android Studio(java Room持久性库)

java.lang.ExceptionInInitializerError-Android Studio(java Room持久性库),java,android-studio,parsing,android-room,android-imageview,Java,Android Studio,Parsing,Android Room,Android Imageview,我目前正在开发一个情绪跟踪手机应用程序,它看起来像这样,使用房间数据库 MVVM体系结构(Dao数据库和存储库)是按照这一点构建的。我找到了一个用Kotlin语言编写的表情选择器。然而,我对移动应用开发非常陌生,我尝试自己修改它,但不幸的是,我得到了很多错误,我不知道如何存储选定的表情,我把所有5个表情作为可绘制选择器,其中选定的一个将存储在db中。然而,我的应用程序在emulator上运行时崩溃,这是logcat中的错误消息 2021-04-06 18:52:36.299 8608-8608

我目前正在开发一个情绪跟踪手机应用程序,它看起来像这样,使用房间数据库

MVVM体系结构(Dao数据库和存储库)是按照这一点构建的。我找到了一个用Kotlin语言编写的表情选择器。然而,我对移动应用开发非常陌生,我尝试自己修改它,但不幸的是,我得到了很多错误,我不知道如何存储选定的表情,我把所有5个表情作为可绘制选择器,其中选定的一个将存储在db中。然而,我的应用程序在emulator上运行时崩溃,这是logcat中的错误消息

2021-04-06 18:52:36.299 8608-8608/com.example.mood E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mood, PID: 8608
    java.lang.ExceptionInInitializerError
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NumberFormatException: For input string: "com.example.mood.EXTRA_EMOJI"
        at java.lang.Integer.parseInt(Integer.java:615)
        at java.lang.Integer.parseInt(Integer.java:650)
        at com.example.mood.AddMoodActivity.<clinit>(AddMoodActivity.java:29)
        at java.lang.Class.newInstance(Native Method) 
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) 
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45) 
        at android.app.Instrumentation.newActivity(Instrumentation.java:1253) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

MoodAdapter.java

public class MoodAdapter extends RecyclerView.Adapter<MoodAdapter.MoodHolder> {

    private List<Mood> moods = new ArrayList<>();

    @NonNull
    @Override
    public MoodHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.mood_item,parent,false);
        return new MoodHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MoodHolder holder, int position) {
        Mood currentMood = moods.get(position);
        holder.date.setText(currentMood.getDate());
        holder.emoji.setImageResource(currentMood.getEmoji());
        holder.journal_text.setText(currentMood.getJournal_text());
    }

    @Override
    public int getItemCount() {
        return moods.size();
    }

    public void setMoods(List<Mood> moods){
        this.moods = moods;
        notifyDataSetChanged();
    }

    //swipe to delete
    public Mood getMoodAt (int position){
        return moods.get(position);
    }

    class MoodHolder extends RecyclerView.ViewHolder{
        private TextView date;
        private ImageView emoji;
        private TextView journal_text;

        public MoodHolder(@NonNull View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.date);
            emoji = itemView.findViewById(R.id.emoji);
            journal_text = itemView.findViewById(R.id.journal_text);
        }
    }
}
您正在尝试将“com.example.mood.EXTRA_EMOJI”解析为int,如下所示:-

Caused by: java.lang.NumberFormatException: For input string: "com.example.mood.EXTRA_EMOJI"
  • 注意由引起的短语的使用
AddMoodActivity.java
中,请参见第行

public static final int EXTRA_EMOJI = Integer.parseInt("com.example.mood.EXTRA_EMOJI");
解决办法是

  • 使要分析的字符串成为有效的int值(如“1”)

  • (无需解析,因为您只需设置值即可)例如,
    public static final int EXTRA_EMOJI=1
  • 为变量使用适当的类型,例如String例如
    public static final String EXTRA\u EMOJI=“com.example.mood.EXTRA\u EMOJI”

  • 可能是后者考虑了
    data.putExtra(String.valueOf(EXTRA_EMOJI),EMOJI),并且IntentputExtra方法的第一个参数是键(标识符)

  • public class AddMoodActivity extends AppCompatActivity {
    
        public static final String EXTRA_DATE = "com.example.mood.EXTRA_DATE";
        public static final int EXTRA_EMOJI = Integer.parseInt("com.example.mood.EXTRA_EMOJI");
        public static final String EXTRA_JOURNAL = "com.example.mood.EXTRA_JOURNAL";
    
        private static final String TAG = "AddMoodActivity";
    
        private EditText pick_date, edit_journal;
        private ImageView edit_emoji;
        private ImageView happy_selected, blessed_selected, moody_selected, neutral_selected, sad_selected;
        private DatePickerDialog.OnDateSetListener onDateSetListener;
    
        Button btnDatePicker;
        private int emoji_selected = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_mood);
    
            pick_date = findViewById(R.id.pick_date);
            edit_emoji = findViewById(R.id.emoji);
            edit_journal = findViewById(R.id.edit_journal_text);
            btnDatePicker = findViewById(R.id.btn_date);
            happy_selected = findViewById(R.id.happy_selected);
            blessed_selected = findViewById(R.id.blessed_selected);
            moody_selected = findViewById(R.id.moody_selected);
            neutral_selected = findViewById(R.id.neutral_selected);
            sad_selected = findViewById(R.id.sad_selected);
    
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
            setTitle("Add New Mood Entry");
    
            //select Date
            btnDatePicker.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Calendar cal = Calendar.getInstance();
                    int year = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH);
                    int day = cal.get(Calendar.DAY_OF_MONTH);
    
                    DatePickerDialog dialog = new DatePickerDialog(
                            AddMoodActivity.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                            onDateSetListener,
                            year,month,day);
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    dialog.show();
                }
            });
    
            //set chosen date
            onDateSetListener = new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Log.d(TAG, "onDateSet: dd/mm/yyyy "+ dayOfMonth + "/" + monthOfYear + "/" + year);
    
                    //String month1 = DateFormat.getDateInstance(DateFormat.SHORT).format(month);
                    //SimpleDateFormat date1 = new SimpleDateFormat("dd MMM YYYY");
                    String date = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
                    pick_date.setText(date);
                }
            };
        }
    
    
        //a selector to pick the emoji which will be displayed in recycler view
        private void emojiSelected(){
            happy_selected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    happy_selected.isSelected();
                    emoji_selected = R.drawable.happy;
                }
            });
    
            blessed_selected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    blessed_selected.isSelected();
                    emoji_selected = R.drawable.blessed;
                }
            });
    
            neutral_selected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    neutral_selected.isSelected();
                    emoji_selected = R.drawable.neutral;
                }
            });
    
            moody_selected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    moody_selected.isSelected();
                    emoji_selected = R.drawable.moody;
                }
            });
    
            sad_selected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sad_selected.isSelected();
                    emoji_selected = R.drawable.sad;
                }
            });
        }
    
    
        private void saveMood(){
    
            String date = pick_date.getText().toString();
            int emoji = edit_emoji.getId();
            String journal_text = edit_journal.getText().toString();
    
            if (date.trim().isEmpty() /*|| emoji.trim().isEmpty()*/){
                Toast.makeText(this,"Please insert data",Toast.LENGTH_LONG).show();
                return;
            }
            Intent data = new Intent();
            data.putExtra(EXTRA_DATE, date);
            data.putExtra(String.valueOf(EXTRA_EMOJI), emoji);
            data.putExtra(EXTRA_JOURNAL, journal_text);
    
            setResult(RESULT_OK,data);
            finish();
        }
    
    
        //menu to direct to new mood entry form's layout
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.menu.add_mood_menu, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.save_mood:
                    saveMood();
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
    }
    
    Caused by: java.lang.NumberFormatException: For input string: "com.example.mood.EXTRA_EMOJI"
    
    public static final int EXTRA_EMOJI = Integer.parseInt("com.example.mood.EXTRA_EMOJI");