2

我正在尝试构建一个小型应用程序,该应用程序在外部存储中获取数据文件并通过电子邮件发送。我在 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);
        }

    }

  } 

——新班级

公共类发送数据扩展活动{私有静态最终字符串标签=“主活动”;

/* 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

4 回答 4

5

找到最低的“Caused by”并继续向下直到到达代码中的一行:

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 具有有效上下文之前作为类变量或在构造函数中执行某些操作......但具体问题在第 36 行。

我猜您正在尝试使用this. onCreate()而是在内部初始化您的 Intent 。


找到了。改变这个:

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

到:

Intent intentgetData;

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

intentgetData = new Intent(MainActivity.this, SendData.class);
于 2012-11-13T20:16:32.077 回答
0

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

11-13 13:29:37.343: E/AndroidRuntime(3319):     at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
于 2012-11-13T20:16:57.493 回答
0

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

于 2012-11-13T20:17:10.860 回答
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);
于 2012-11-13T20:25:46.437 回答