0

我尝试将一些数据保存在 android 的内部存储中,但我不断收到 NullPointerException,我认为这与我正在使用的 getFilesDir() 有关,但我不确定。可以请帮助澄清是否是这种情况,并帮助我将此文件写入设备。这是我收到的错误消息

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

这是面板类中的我的 OnTouch 方法

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

这是我的主类中的 writeObject 方法

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

显现

<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>
4

2 回答 2

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);
    ...
}
于 2012-01-21T04:49:22.670 回答
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>
于 2012-01-21T04:33:52.497 回答