2015-06-04 49 views
0

我在android中创建一个简单的笔记应用程序。我基本上使用了两个名为主页面和第二活动的活动类。我将一些数据存储在第二个活动的共享首选项中,并希望在我的第一个活动中使用它。我将数据作为(String,Integer)密钥对存储在共享首选项中。在我的主要活动类中,当我从Integer中获取共享首选项的值并将其与值0进行比较时,发现java.lang.string不能转换为java.lang.integer。我不知道为什么这个例外即将到来。 Android工作室不给我一个例外,但当我运行我的应用程序,我的应用程序崩溃。我已附上代码以供参考。获取投射异常Android

MainActivity类别:

package com.example.prateeksharma.noteballondor; 

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 


public class MainPage extends ActionBarActivity { 

    Notes notes = null; 


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


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main_page, menu); 
     return true; 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     SharedPreferences sharedPreferences = getSharedPreferences("com.example.prateeksharma.noteballondor", Context.MODE_PRIVATE); 
     //Map<String, Integer> notesMap = (Map<String, Integer>)sharedPreferences.getAll(); 
     List<Notes> listNotes = new ArrayList<>(); 
     Map<String, ?> prefsMap = sharedPreferences.getAll(); 
     for (Map.Entry<String, ?> entry: prefsMap.entrySet()) { 
      if (entry.getValue() == 0){ 
       notes = new Notes(); 
      } 
      notes.setNoteText(entry.getKey()); 
      listNotes.add(notes); 
      sharedPreferences.edit().putInt(entry.getKey(),2); 
     } 
     NotesArrayAdapter notesArrayAdapter = new NotesArrayAdapter(this,R.layout.list_layout, listNotes); 
     final ListView listview = (ListView) findViewById(R.id.listView); 
     listview.setAdapter(notesArrayAdapter); 
     listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
/**/ 
      @Override 
      public void onItemClick(AdapterView<?> parent, final View view, 
            int position, long id) { 
       notes = (Notes) parent.getItemAtPosition(position); 
       Intent intent = new Intent(MainPage.this, SecondActivity.class); 
       intent.putExtra("Notes",notes); 
       startActivity(intent); 
      } 
     }); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     switch (item.getItemId()) { 
      case R.id.new_link: 
       Intent intent = new Intent(this, SecondActivity.class); 
       intent.putExtra("Notes",(android.os.Parcelable)null); 
       startActivity(intent); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 

    } 
} 

SecondActivity类:

package com.example.prateeksharma.noteballondor; 

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.EditText; 


public class SecondActivity extends ActionBarActivity { 

    public SharedPreferences sharedPreferences; 
    SharedPreferences.Editor edit; 
    Notes notes; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 
     sharedPreferences = getSharedPreferences("com.example.prateeksharma.noteballondor", Context.MODE_PRIVATE); 
     edit = sharedPreferences.edit(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_second, menu); 
     return true; 
    } 


    @Override 
    protected void onResume() { 
     super.onResume(); 
     Intent intent = getIntent(); 
     notes = (Notes)intent.getParcelableExtra("Notes"); 
     EditText editText = (EditText) findViewById(R.id.editText); 
     if(notes != null){ 
      editText.setText(notes.getNoteText()); 
     } 
     else{ 
      editText.setText(null); 
     } 

    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     getIntent().removeExtra(notes.getNoteText()); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     Intent intent = getIntent(); 

     //noinspection SimplifiableIfStatement 
     switch (item.getItemId()) { 
      case R.id.save: 
       EditText editText = (EditText)findViewById(R.id.editText); 
       if (notes != null){ 
        edit.putInt(String.valueOf(editText.getText()),1); 
       } 
       else{ 
        edit.putInt(String.valueOf(editText.getText()),0); 
       } 
       edit.commit(); 
       return true; 
      case R.id.delete: 
       if(notes != null){ 
        edit.remove(notes.getNoteText()); 
        edit.commit(); 
        finish(); 
        return true; 
       } 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
} 

票据类,我使用的是:

package com.example.prateeksharma.noteballondor; 

import android.os.Parcel; 
import android.os.Parcelable; 

/** 
* Created by Dell on 02-06-2015. 
*/ 
    public class Notes implements Parcelable { 

     private String noteText; 

     public String getNoteText() { 
      return noteText; 
     } 

     public void setNoteText(String noteText) { 
      this.noteText = noteText; 
     } 


     private Notes(Parcel in) { 
      noteText = in.readString(); 
     } 

     public Notes(){ 

     } 

     @Override 
     public int describeContents() { 
      return 0; 
     } 

     @Override 
     public void writeToParcel(Parcel dest, int flags) { 
      dest.writeString(noteText); 
     } 

     @SuppressWarnings("unused") 
     public static final Parcelable.Creator<Notes> CREATOR = new Parcelable.Creator<Notes>() { 
      @Override 
      public Notes createFromParcel(Parcel in) { 
       return new Notes(in); 
      } 

      @Override 
      public Notes[] newArray(int size) { 
       return new Notes[size]; 
      } 
     }; 
    } 

谁能告诉我为什么这个错误发生在MainActivity类别来在该行 如果(entry.getValue()== 0)

谢谢。

+0

这可能是因为当你定义'地图 prefsMap = sharedPreferences.getAll();'你包括所有存储在您的SharedPrefe值的rences,其中包括您在'onOptionsItemSelected'方法中保存的'noteText'字符串,这肯定会导致ClassCastException,因为您试图将存储在您的首选项中的每个项目都作为整数进行投射。 – Guardanis

+0

@Guardians对不起..但我不明白你的答案。在第二个Activity类的onOptionsItemSelected方法中,我将key-pair值存储为sharedPreferences中的(String,Integer)。所以value应该总是整数。我正在使用sharedPreferece类的putInt方法。在主Activity类中,我正在执行Map.get(Key)以获取应始终为整数的值。如果我错了,可以请你详细说明你的答案或纠正我。 –

回答

0

尝试更换

if ((Integer)entry.getValue() == 0){ 

随着

If((Integer.parseInt(entry.getValue()) ==0){ 
+0

Nilesh entry.getvalue()是一个整数值,我可以在整数参数上使用Integer.parseInt()。我也编辑了我的问题来删除整数投射,因为没有必要这样做。 –