2012-01-21 5 views
0

안드로이드의 내부 저장소에 데이터를 저장하려고하는데 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> 
+0

어느 라인이 라인 main.java:89입니까? – ghostbust555

+1

파일을 AndroidMenifest.xml에 FILE 읽기 권한으로 지정 하시겠습니까? – Lucifer

+0

@ ghostbust555 89 행은 "File f = 새 파일 (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

관련 문제