getFilesDir() 方法中出现 NullPointerException 错误
我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道出了什么问题。您不能只编写
getFilesDir 应该获取正确的上下文实例,但现在不行。
尝试这样的事情:
I know what's wrong. You can't just write
getFilesDir should get the correct context instance but it doesn't now.
Try something like that:
请尝试
打开 AndroidManifest.xml 并在标签结束后写入以下行。
Please try,
Open your AndroidManifest.xml and write following lines after the closing of tag.