getFilesDir() 方法中出现 NullPointerException 错误

发布于 2024-12-28 02:24:35 字数 3282 浏览 0 评论 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);
 }

这是我的主类

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>

I try to save some data in internal storage on android but I keep getting a NullPointerException I think it has to do with the getFilesDir() I'm using but I'm not sure. Can Some Please help clarify if that is that case and help me write this file to the device. Here the Error message im am getting

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)

Here is the oncreate of the main class

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

Here my OnTouch method in the panel class

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

Here is my writeObject method inside my main class

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

manifest

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2025-01-04 02:24:35

我知道出了什么问题。您不能只编写

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

I know what's wrong. You can't just write

mainactivity=new main();

getFilesDir should get the correct context instance but it doesn't now.
Try something like that:

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);
    ...
}
冷情 2025-01-04 02:24:35

请尝试

打开 AndroidManifest.xml 并在标签结束后写入以下行。

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

Please try,

Open your AndroidManifest.xml and write following lines after the closing of tag.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文