当我尝试写入外部文件时,为什么我的应用程序崩溃了?

时间:2014-06-10 14:29:03

标签: android

在此活动中,我尝试将字符串保存到外部文件以响应按下的按钮。我一直在使用关于写入外部文件的其他帖子,但我尝试的每个都是崩溃我的应用程序。我也不确定我的AVD是否是问题的根源并且具有适当的设置。有人可以看看我的AVD设置或我的活动文件中是否有任何错误?提前谢谢。

以下是我的设置(无法发布图片):

设备:10.1" WXGA(平板电脑)(1280 x 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"/>

1 个答案:

答案 0 :(得分:1)

onClick Listener方法签名错误:它错过了View参数

public void q1a1(){

应该是

public void q1a1(View v){

<强> [编辑]

通过阅读新的LogCat帖子,您会发现您从不为电视分配价值 类似的东西:

tv = (TextView) findViewById(R.id.myTextView);
应在此行之后添加

setContentView(R.layout.fragment_fourth);
你的onCreate方法中的