java.lang.RuntimeException:无法使用TextView启动活动ComponentInfo

时间:2012-02-13 20:27:21

标签: android

我正在制作文件IO项目,我的组件视图出现问题。我知道它有我的TextView的东西,但我是一个新手,不能弄明白。以下是代码:

package com.IPR2.viewlog;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;



public class Main extends Activity {
    public TextView tv;

    /** Called when the activity is first created. */


    @Override
    public void onCreate(Bundle savedInstanceState) {
        FileOperations fileOperations = new FileOperations();
        Testing testing = new Testing();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);
        testing.checkExternalMedia();
        //fileOperations.CreateFile("example.txt");



    }



}

这是Log for it:

    02-13 19:54:47.199: W/dalvikvm(1533): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-13 19:54:47.209: E/AndroidRuntime(1533): FATAL EXCEPTION: main
02-13 19:54:47.209: E/AndroidRuntime(1533):  {com.IPR2.viewlog/com.IPR2.viewlog.ViewLog}: java.lang.NullPointerException
02-13 19:54:47.209: E/AndroidRuntime(1533):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at android.os.Looper.loop(Looper.java:130)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at java.lang.reflect.Method.invoke(Method.java:507)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at dalvik.system.NativeStart.main(Native Method)
02-13 19:54:47.209: E/AndroidRuntime(1533): Caused by: java.lang.NullPointerException
02-13 19:54:47.209: E/AndroidRuntime(1533):     at com.IPR2.viewlog.Testing.checkExternalMedia(Testing.java:43)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at com.IPR2.viewlog.ViewLog.onCreate(ViewLog.java:31)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-13 19:54:47.209: E/AndroidRuntime(1533):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-13 19:54:47.209: E/AndroidRuntime(1533):     ... 11 more

我正在使用另一个类并调用tv.append(“File not saved!”)作为调试。如果我发表评论,问题就会消失。我认为我错误地处理它并且知道它应该很容易修复。我知道我可以使用其他方式进行调试,但我想学习如何以这种方式进行调试,因为我希望再也不会出现此错误:]

package com.IPR2.viewlog;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import android.os.Environment;
import android.widget.Toast;

public class FileOperations extends Main{



        public void CreateFile(String fileName)
        {
        try {
            File root = new File(Environment.getExternalStorageDirectory(),
                    "Nullwall");
            if (!root.exists()) {
                root.mkdirs();
            }
            tv.append("\nLog File Created In Directory: " + root);
            File gpxfile = new File(root, fileName);
            FileWriter writer = new FileWriter(gpxfile, true);
            writer.write("");
            writer.flush();
            writer.close();
            Toast.makeText(this, fileName + " Log Successfully Created!",
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            tv.append(" \n File not Saved! CreateLogFile() Broken!");
        }
    }

1 个答案:

答案 0 :(得分:0)

package com.IPR2.viewlog;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;



public class Main extends Activity {
    public TextView tv;

    /** Called when the activity is first created. */


    @Override
    public void onCreate(Bundle savedInstanceState) {
        FileOperations fileOperations = new FileOperations();
        Testing testing = new Testing();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);
        //testing.checkExternalMedia();
        createFile("example.txt");
    }


    public void createFile(String fileName)
    {
        try {
            File root = new File(Environment.getExternalStorageDirectory(),
                    "Nullwall");
            if (!root.exists()) {
                root.mkdirs();
            }
            tv.append("\nLog File Created In Directory: " + root);
            File gpxfile = new File(root, fileName);
            FileWriter writer = new FileWriter(gpxfile, true);
            writer.write("");
            writer.flush();
            writer.close();
            Toast.makeText(this, fileName + " Log Successfully Created!",
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            tv.append(" \n File not Saved! CreateLogFile() Broken!");
        }
}

我不知道这段代码是否有效。但它应该是。所以现在我会尝试解释它。 如果您创建一个TextView对象tv,那么您应该从同一个类引用它(或者您可以生成一个方法来访问它)。

其次,您应该改进您的架构。如果一个名为FileOperations的类,那么我希望看到有方法和字段来处理文件。 FileOperations不适合更新UI。

第三,如果只有一种方法,为什么要为它创建一个单独的类?

代码中的问题是无法从派生类访问tv。