안드로이드의 내부 저장소에 데이터를 저장하려고하는데 NullPointerException이 계속 발생합니다. 사용하고있는 getFilesDir()과 관련이 있다고 생각하지만 확실하지 않습니다. 일부는 그 경우 명확하게 설명하고이 파일을 장치에 쓰도록 도울 수 있습니까? 다음은 오류 메시지의 메신저 메인 클래스의 한 OnCreate는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)
다음
입니다 얻고있다
다음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>
어느 라인이 라인 main.java:89입니까? – ghostbust555
파일을 AndroidMenifest.xml에 FILE 읽기 권한으로 지정 하시겠습니까? – Lucifer
@ ghostbust555 89 행은 "File f = 새 파일 (getFilesDir() + FILENAME);" 코드에 라인이 있습니다. –