如何在两个List <e>项之间复制字段?</e>

时间:2014-05-07 07:38:10

标签: android list android-package-managers

我想将 packageList1 的内容复制到类packageList_debug 的字段mainPackageInfo中。以下是我的代码&amp;也是我失败的堆栈跟踪。 (应用程序崩溃)

我在这里做错了什么?

onCreate 函数中的主要Activity:

List<PackageInfo> packageList1;
List<MyPackageInfo> packageList_debug;
packageManager = getPackageManager();
packageList1 = packageManager.getInstalledPackages(0);

MyPackageInfo temp_MyPackageInfo = null;
    for(int i=0 ; i < packageList1.size() ; i++) {
        temp_MyPackageInfo.setMainPackageInfo(packageList1.get(i));
        temp_MyPackageInfo.setSelected(true);
        packageList_debug.set(i, temp_MyPackageInfo);
    }   

失败的行号(根据下面显示的Stacktrace)是556,这是上面代码中的以下行:     的 temp_MyPackageInfo.setMainPackageInfo(packageList1_l.get(I));

我的自定义类 MyPackageInfo

import android.content.pm.PackageInfo;

public class MyPackageInfo {

private PackageInfo mainPackageInfo;
private boolean selected;

public MyPackageInfo(PackageInfo mainPackageInfo) {
    super();
    this.mainPackageInfo = mainPackageInfo;
}

public PackageInfo getMainPackageInfo() {
    return mainPackageInfo;
}

public void setMainPackageInfo(PackageInfo mainPackageInfo) {
    this.mainPackageInfo = mainPackageInfo;
}

public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}
}

我的Stacktrace错误失败

05-07 02:30:39.715: E/AndroidRuntime(3975): FATAL EXCEPTION: main
05-07 02:30:39.715: E/AndroidRuntime(3975): java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.mavdev.focusoutfacebook/com.mavdev.focusoutfacebook.MainActivityCircularSeekbar}:     java.lang.NullPointerException
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.app.ActivityThread.access$700(ActivityThread.java:165)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.os.Looper.loop(Looper.java:137)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.app.ActivityThread.main(ActivityThread.java:5455)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at java.lang.reflect.Method.invoke(Method.java:525)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at dalvik.system.NativeStart.main(Native Method)
05-07 02:30:39.715: E/AndroidRuntime(3975): Caused by: java.lang.NullPointerException
05-07 02:30:39.715: E/AndroidRuntime(3975):     at com.mavdev.focusoutfacebook.MainActivityCircularSeekbar.copyPACKAGELIST1toMYPACKAGEINFO(MainActivityCircularSeekbar.java:556)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at com.mavdev.focusoutfacebook.MainActivityCircularSeekbar.onCreate(MainActivityCircularSeekbar.java:542)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.app.Activity.performCreate(Activity.java:5372)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-07 02:30:39.715: E/AndroidRuntime(3975):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
05-07 02:30:39.715: E/AndroidRuntime(3975):     ... 11 more

2 个答案:

答案 0 :(得分:2)

这是因为您刚刚为MyPackageInfo temp_MyPackageInfo = null;声明了未初始化的变量。所以你需要初始化。

temp_MyPackageInfo = new MyPackageInfo(); 

需要在MyPackageInfo类中使用

作为构造函数
public void MyPackageInfo() {

}

答案 1 :(得分:1)

当你进入循环时,

temp_MyPackageInfo为空,因此该行失败:

temp_MyPackageInfo.setMainPackageInfo(packageList1.get(i));

您应该创建一个默认构造函数并首先对其进行初始化