在构造函数中传递上下文

时间:2014-04-20 11:11:52

标签: android

除了我在这里开始的主题

Android: openFileOutput throws NullPointerException

好的,所以,首先我将上下文直接传递给listToTextFile函数,它运行正常。之后我决定将上下文作为参数传递给GenerateXml类的构造函数,如下所示:

    public GenerateXml(Context cntx){

        this.cntx = cntx;
    }

我得到了NullPointerException agian。 调试时,我可以看到构造函数中的赋值没有效果,“this.cntx”实际上是NULL。

主要活动调用GenerateXml代码:

private void parseAppListToXML(List<ApplicationInfo> packages) {
        // TODO Auto-generated method stub
        GenerateXml gXml = new GenerateXml(getApplicationContext());
        gXml.listToXml();
    }

LogCat显示NullPointerException - 可能是因为this.cntx为Null:

04-20 11:20:17.580: E/AndroidRuntime(368): FATAL EXCEPTION: main
04-20 11:20:17.580: E/AndroidRuntime(368): java.lang.NullPointerException
04-20 11:20:17.580: E/AndroidRuntime(368):  at com.example.tester.GenerateXml.<init>(GenerateXml.java:32)
04-20 11:20:17.580: E/AndroidRuntime(368):  at com.example.tester.MainActivity$1.parseAppListToXML(MainActivity.java:80)
04-20 11:20:17.580: E/AndroidRuntime(368):  at com.example.tester.MainActivity$1.onClick(MainActivity.java:62)
04-20 11:20:17.580: E/AndroidRuntime(368):  at android.view.View.performClick(View.java:2408)
04-20 11:20:17.580: E/AndroidRuntime(368):  at android.view.View$PerformClick.run(View.java:8816)
04-20 11:20:17.580: E/AndroidRuntime(368):  at android.os.Handler.handleCallback(Handler.java:587)
04-20 11:20:17.580: E/AndroidRuntime(368):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 11:20:17.580: E/AndroidRuntime(368):  at android.os.Looper.loop(Looper.java:123)
04-20 11:20:17.580: E/AndroidRuntime(368):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-20 11:20:17.580: E/AndroidRuntime(368):  at java.lang.reflect.Method.invokeNative(Native Method)
04-20 11:20:17.580: E/AndroidRuntime(368):  at java.lang.reflect.Method.invoke(Method.java:521)
04-20 11:20:17.580: E/AndroidRuntime(368):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-20 11:20:17.580: E/AndroidRuntime(368):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-20 11:20:17.580: E/AndroidRuntime(368):  at dalvik.system.NativeStart.main(Native Method)

班级:

package com.example.tester;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.ListIterator;

import android.content.Context;
import android.content.pm.ApplicationInfo;
//import android.util.Xml;
import android.content.pm.PackageManager;

public class GenerateXml  {




//
// Attributes
//

protected Context cntx;

//Constructor gets caller Context

    public GenerateXml(Context cntx){

        this.cntx = cntx;
    }

final PackageManager pm = cntx.getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);


public void listToXml() {

    // break List into array so late will print one line at a time to the file
    ListIterator<ApplicationInfo> iter = packages.listIterator();

    try { // catches IOException below

         FileOutputStream fOut =  cntx.openFileOutput("AppsList.XML",Context.MODE_PRIVATE);

         OutputStreamWriter osw = new OutputStreamWriter(fOut); 
         osw.write("<INSTALLED_APPS_LIST>");
         while (iter.hasNext()) {
         // Write the string to the file

             osw.write("<APPLICATION>");
             osw.write("<PACKAGE>");
             osw.write(iter.next().toString());
             osw.write("</PACKAGE>");
            // osw.write(iter.);
             osw.write("</APPLICATION>");
             osw.write('\n');
         /* ensure that everything is
          * really written out and close */
         }
         osw.write("</INSTALLED_APPS_LIST>");

         osw.flush();
         osw.close();
     }catch (IOException e){
         //Log.e(TAG,"could not open file out stream", e); 
     }
}
}

???

1 个答案:

答案 0 :(得分:1)

final PackageManager pm = cntx.getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

在构造函数运行之前初始化成员变量,并且只在构造函数中初始化cntx。因此NPE。将这些初始化也移动到构造函数中。