2014-06-10 83 views
-1

在此活動中,我嘗試將字符串保存到外部文件以響應按下的按鈕。我一直在使用關於寫入外部文件的其他文章,但我嘗試的每一個都會崩潰我的應用程序。我也不確定我的AVD是否是問題的根源,並且具有適當的設置。有人可以看看我的AVD設置或我的活動文件是否有錯誤嗎?先謝謝你。當我嘗試寫入外部文件時,爲什麼我的應用崩潰?

這裏是我的設置(無法尚未發表圖片):

設備:10.1" WXGA(片劑)(1280×800 MDPI)

目標:是Android 4.4.2-API等級19

CPU/ABI:ARM(armeabi-V7A)

本硬件鍵盤

皮膚:皮膚用動態硬件控制

前置攝像頭:Webcam0

後置攝像頭:Webcam0

內存選項:RAM:512 VM堆:32

內部存儲:200 MIB

SD卡:尺寸:200 MIB

這是我活動的java文件:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.PrintWriter; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class FourthActivity extends Activity { 

private static final String TAG = "MEDIA"; 
private TextView tv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_fourth); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.fourth, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 



/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_fourth, 
       container, false); 
     return rootView; 
    } 
} 

    /** Called when the user clicks an answer */ 

public void q1a1(){ 
    File root = android.os.Environment.getExternalStorageDirectory(); 
    tv.append("\nExternal file system root: "+root); 

    File dir = new File (root.getAbsolutePath() + "/download"); 
    dir.mkdirs(); 
    File file = new File(dir, "myData.txt"); 

    try { 
     FileOutputStream f = new FileOutputStream(file); 
     PrintWriter pw = new PrintWriter(f); 
     pw.println("@string/q1a1"); 
     pw.flush(); 
     pw.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Log.i(TAG, "***** File not found. Did you" + "add a permission to the manifest?"); 
    } 
    tv.append("\n\nFile written to "+file); 

    Intent intent = new Intent(FourthActivity.this, FifthActivity.class); 
    startActivity(intent); 
} 

我相信我的佈局文件沒問題,但如果有人願意也可以添加它。

以下是錯誤日誌:

06-10 10:40:44.311: E/AndroidRuntime(4857):  at android.view.View.performClick(View.java:4438) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at android.view.View$PerformClick.run(View.java:18422) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at android.os.Handler.handleCallback(Handler.java:733) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at android.os.Handler.dispatchMessage(Handler.java:95) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at android.os.Looper.loop(Looper.java:136) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at java.lang.reflect.Method.invoke(Method.java:515) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at dalvik.system.NativeStart.main(Native Method) 
06-10 10:40:44.311: E/AndroidRuntime(4857): Caused by: java.lang.reflect.InvocationTargetException 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at java.lang.reflect.Method.invoke(Method.java:515) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at android.view.View$1.onClick(View.java:3818) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  ... 11 more 
06-10 10:40:44.311: E/AndroidRuntime(4857): Caused by: java.lang.NullPointerException 
06-10 10:40:44.311: E/AndroidRuntime(4857):  at com.example.motorimagery.FourthActivity.q1a1(FourthActivity.java:75) 
06-10 10:40:44.311: E/AndroidRuntime(4857):  ... 14 more 

下面是XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

    <TextView 
    android:layout_width="736dp" 
    android:layout_height="226dp" 
    android:text="@string/Question1" 
    android:textSize="40sp" /> 

    <Button 
    android:layout_width="200dp" 
    android:layout_height="100dp" 
    android:onClick="q1a1" 
    android:text="@string/Q1A1" 
    android:gravity="center" 
    android:textSize="40sp" 
    android:clickable="true"/> 

+0

您可以發佈錯誤日誌 – Zubair

+1

你有寫權限到外部存儲? – Dimitri

+0

我在清單Dimitri中有權限聲明。 – dy19

回答

1

的onclick監聽方法簽名是錯誤的:它忽略View參數

public void q1a1(){ 

應該是

public void q1a1(View v){ 

[編輯]

通過閱讀你的新logcat的發佈,它出現了,你從來沒有分配電視的價值。
喜歡的東西:

tv = (TextView) findViewById(R.id.myTextView); 

應此行後添加:

setContentView(R.layout.fragment_fourth); 

在onCreate方法

+0

如果他在onClicklistener中調用q1a1()函數呢?它會錯嗎?只是好奇 –

+0

是的,簽名應該是:'public void onClick(View v)' - 只有名稱改變。它會有'@ Overrides'修飾符在它上面。 –

+0

添加視圖沒有幫助。我應該試試嗎? – dy19

相關問題