如何使用logcat查找空指针异常

时间:2012-11-13 20:12:49

标签: java android

我正在尝试构建一个小应用程序,它将数据文件放在外部存储中并通过电子邮件发送给它。我一直在logcat中得到'空指针异常'然后我的应用程序死了。我似乎无法找到我的异常,我想知道是否有办法确定造成这种情况的代码行。我有MainActivity类,然后是一个名为SendData的类 - 代码如下。我是新来的,所以任何帮助都会非常感谢 - 谢谢。

    private static final String TAG = "MainActivity_ErrorLog";

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

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

// Create the getData intent
    Intent intentgetData = new Intent(MainActivity.this, SendData.class); 

    public void onStart()
    {
        {       
        try 
        {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

            if (sd.canWrite()) 
            {
        // verify the paths
                String currentDBPath = "TLC_COMMON/database.db";
                String backupDBPath = "database.db"; 
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) 
                {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();                    
                }
            }
        }

        catch (Exception e)     
        {   
            // change the V below to E when complete
            Log.v(TAG,"ERROR: Database file not created"); 
        }
        startActivity(intentgetData);
        }

    }

  } 

- 新班级

public class SendData extends Activity {     private static final String TAG =“MainActivity”;

/* Checks if external storage is available to read */   
public boolean isExternalStorageReadable() 
    {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
        {
        return true;
        }
        return false;
    }

/** Called when the user clicks the Send My Data button */
public SendData(View view) 
    {

    // Send data by email
        {
            File root = Environment.getExternalStorageDirectory(); 
    // verify it is saving as this file name; also path in previous class
            String fileName = "database.db"; 
            if (root.canWrite()) 
                { 
                File attachment = new File(root, fileName); 
                Intent email = new Intent(Intent.ACTION_SENDTO);                
                email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Exercise data");
                email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"});
                // is the Uri necessary?
                email.putExtra(android.content.Intent.EXTRA_TEXT, Uri.fromFile(attachment));
                // look at this VVV
                startActivity(Intent.createChooser(email, "Send the data via Email"));} 
            else 
                {
                // Change the V below to E when complete
                Log.v(TAG, "Email send failed");
                }
            }
        }
public void finish() 
{   
}

}

11-13 13:29:37.343: W/dalvikvm(3319): threadid=1: thread exiting with uncaught exception (group=0x418e3300)
11-13 13:29:37.343: E/AndroidRuntime(3319): FATAL EXCEPTION: main
11-13 13:29:37.343: E/AndroidRuntime(3319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.va.datasender/com.example.va.datasender.MainActivity}: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.os.Looper.loop(Looper.java:137)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at java.lang.reflect.Method.invoke(Method.java:511)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at dalvik.system.NativeStart.main(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): Caused by: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.content.ComponentName.<init>(ComponentName.java:75)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.content.Intent.<init>(Intent.java:3301)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at java.lang.Class.newInstanceImpl(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at java.lang.Class.newInstance(Class.java:1319)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-13 13:29:37.343: E/AndroidRuntime(3319):     ... 11 more

4 个答案:

答案 0 :(得分:5)

找到最低的“由...引起”并继续向下,直到您从代码中找到一行:

Caused by: java.lang.NullPointerException
     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
     at android.content.ComponentName.<init>(ComponentName.java:75)
     at android.content.Intent.<init>(Intent.java:3301)
     at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)

<init>表示你在Activity有一个有效的Context之前作为一个类变量或在构造函数中做某事......但具体的问题是在第36行。

我猜您正在尝试使用this创建一个Intent。在onCreate()内初始化您的意图。


找到它。改变这个:

// Create the getData intent
Intent intentgetData = new Intent(MainActivity.this, SendData.class); 

为:

Intent intentgetData;

onCreate()内部(或在调用onStart()之前startActivity())添加:

intentgetData = new Intent(MainActivity.this, SendData.class);

答案 1 :(得分:0)

您的问题来自MainActivity上的第36行。检查那里的问题。

11-13 13:29:37.343: E/AndroidRuntime(3319):     at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)

答案 2 :(得分:0)

第36行的例外情况:at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)

答案 3 :(得分:0)

由于错误日志在android.content.Intent.<init>之后也会显示MainActivity.java:36,因此我会在实例化intentgetData时检查以下行中第一个参数的值:

Intent intentgetData = new Intent(MainActivity.this, SendData.class); 

不要在那里完成设置,而是在调用onStart()之前尝试在startActivity(intentgetData);方法中执行此操作:

Intent intentgetData = new Intent(MainActivity.this, SendData.class); 
startActivity(intentgetData);