2012-01-21 33 views
0

我嘗試在android上的內部存儲器中保存一些數據,但我不斷收到NullPointerException我認爲它與我使用的getFilesDir()有關,但我不確定。可以一些請幫助澄清,如果是這種情況,並幫助我寫這個文件到設備。這裏的錯誤消息IM正在逐漸getFilesDir()方法中的NullPointerException錯誤

01-20 22:11:59.020: E/AndroidRuntime(329): FATAL EXCEPTION: main 
01-20 22:11:59.020: E/AndroidRuntime(329): java.lang.NullPointerException 

01-20 22:11:59.020: E/AndroidRuntime(329): at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178) 

01-20 22:11:59.020: E/AndroidRuntime(329): at yantz.imageapp4.main.writeElement(main.java:89) 

01-20 22:11:59.020: E/AndroidRuntime(329): at yantz.imageapp4.Panel.onTouchEvent(Panel.java:102) 

這裏是主類的OnCreate

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FrameLayout sv = new FrameLayout(this); 
    LinearLayout ll = new LinearLayout(this); 
    test = new Panel(this); 
    test.settest(1) ; 
    ll.setOrientation(LinearLayout.VERTICAL); 
    sv.addView(test); 
    sv.addView(ll); 
    setContentView(sv);} 

在這裏,在面板類

public boolean onTouchEvent(MotionEvent event) { 
    mainactivity=new main();  
    mainactivity.writeElement(new Element(getResources(),(int) event.getX(),(int) event.getY())); 
     Log.v("Gesture", "is 1 "); 
     return super.onTouchEvent(event); 
} 

我OnTouch方法

這裏是我的主類

public void writeElement(Element obj){ 
    Log.v("main", "made it to method writeElement"); 
    File f = new File(getFilesDir()+FILENAME); 
    try { 
fos = new FileOutputStream(f); 
    ObjectOutputStream objectwrite = new ObjectOutputStream(fos); 
    objectwrite.writeObject(obj); 
fos.close(); 
Log.v("main", "file was made File "); 

}catch (FileNotFoundException e){ 
    e.printStackTrace(); 
    Log.v("main", "file was not made File not found "); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    Log.v("main", "file was not made File IOException "); 
} 
} 

清單裏我writeObject方法

<uses-sdk android:minSdkVersion="10" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:label="@string/app_name" 
     android:name="yantz.imageapp4.main" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

     <activity android:name=".charactors" android:label="@string/app_name" 
     android:theme="@android:style/Theme.Black"> 
     <intent-filter> 
    <action android:name="yantz.imageapp4.charactors" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
<activity android:name=".main2" android:label="@string/app_name" 
     android:theme="@android:style/Theme.Black"> 
     <intent-filter> 
<action android:name="yantz.imageapp4.main2" /> 
<category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 
+0

哪一行是main.java:89行? – ghostbust555

+1

你有沒有在AndroidMenifest.xml中給FILE讀寫權限? – Lucifer

+0

@ ghostbust555第89行是「File f = new File(getFilesDir()+ FILENAME);」行代碼 –

回答

1

我知道什麼是錯的。你不能只是寫

mainactivity=new main(); 

getFilesDir應該得到正確的上下文實例,但現在沒有。 試試這樣的:

public boolean onTouchEvent(MotionEvent event) { 
    main.writeElement(new Element(...), this.getContext); 
    return super.onTouchEvent(event); 
} 

public static void writeElement(Element obj, Context context){ 
    ... 
    File f = new File(context.getFilesDir(), FILENAME); 
    ... 
} 
+0

似乎解決了NullPointerException謝謝 –

0

請嘗試,

打開你的AndroidManifest.xml中,寫後以下行標籤的關閉。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> 
+0

我向AndroidManifest.xml添加權限,但是我想出了和以前一樣的錯誤 –

+0

你能上傳你的AndroidManiFest.xml文件的代碼嗎? – Lucifer

+0

READ_EXTERNAL_STORAGE - 「隱式授予任何聲明WRITE_EXTERNAL_STORAGE權限的應用程序」。 – Graeme