2015-06-04 48 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()。我也編輯了我的問題來刪除整數投射,因爲沒有必要這樣做。 –