0

这是一个android项目。我正在使用一个数组来构建一个一切正常的微调器。但是,我现在尝试将记录添加到数组的开头,以便在页面加载时不选择任何选项。

我试图只使用一个myarray.add("---")类型的东西,但它因以下错误而崩溃:

11-05 10:50:36.995: D/AndroidRuntime(16763): Shutting down VM
11-05 10:50:36.995: W/dalvikvm(16763): threadid=1: thread exiting with uncaught exception (group=0x412b42a0)
11-05 10:50:37.000: E/AndroidRuntime(16763): FATAL EXCEPTION: main
11-05 10:50:37.000: E/AndroidRuntime(16763): java.lang.NullPointerException
11-05 10:50:37.000: E/AndroidRuntime(16763):    at com.directenquiries.assessment.tool.AddAsset.updateLocalDesc(AddAsset.java:461)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at com.directenquiries.assessment.tool.AddAsset$3.onItemSelected(AddAsset.java:262)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at android.widget.AdapterView.access$200(AdapterView.java:49)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at android.os.Handler.handleCallback(Handler.java:615)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at android.os.Looper.loop(Looper.java:137)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at java.lang.reflect.Method.invokeNative(Native Method)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at java.lang.reflect.Method.invoke(Method.java:511)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-05 10:50:37.000: E/AndroidRuntime(16763):    at dalvik.system.NativeStart.main(Native Method)
11-05 10:50:37.020: D/dalvikvm(16763): GC_CONCURRENT freed 243K, 7% free 12989K/13831K, paused 1ms+3ms, total 25ms

我的代码是:

 public void setupAssetType(Boolean beenSet){
         Log.e("CamDebug", "Loading asset type");
           Cursor c = db.rawQuery("select * from AssetTypes", null);  
           Log.e("Error Tracking", "Loading AssetTypes");
           if(c.getCount() != 0) {
             c.moveToFirst();
             final ArrayList<String> mAssetType = new ArrayList<String>();
             final ArrayList<String> mTypeID= new ArrayList<String>();


               c.moveToFirst();
               mAssetType.add("TEST");
               mTypeID.add("TEST");
               while(!c.isAfterLast()) {
                    mAssetType.add(c.getString(c.getColumnIndex("AssetType")));
                    mTypeID.add(c.getString(c.getColumnIndex("AssetTypeID")));
                    c.moveToNext();
               }

                ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, mAssetType);
                spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
                spnAssetType.setAdapter(spinnerArrayAdapter);
                int count = 0;
                if(beenSet == true) {
                    for (String mID : mTypeID) {

                        Log.e("Set asset type", mID);
                        if(mID.equals(orAssetTypeID)){
                            spnAssetType.setSelection(count);
                            Log.e("Set asset type FOUND", mID);
                            break;
                        }
                        count++;
                        }   
                    }
                    c.close();            
                spnAssetType.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                     Log.e("CamDebug", "Selected asset type");
                        if(mAssetType.size() > 0){
                            setupGroup(mTypeID.get(position), false);   
                            AssetType = mTypeID.get(position);
                            updateLocalDesc();
                            Log.e("Error Tracking", "Before Asset Type set");
                            if(mTypeID.get(position) != null) {
                                AssetTypeID = mTypeID.get(position);
                                } else {
                                    AssetTypeID = "";
                                }
                            Log.e("Error Tracking", "After As");
                        }   
                }

                @Override
                public void onNothingSelected(AdapterView<?> parentView) {

                }

            });

           }
    }

如您所见,我添加了:

mAssetType.add("TEST");
mTypeID.add("TEST");

去测试。但它不起作用。任何想法将不胜感激。

编辑:第 461 行是: AssetGroup = spnAssetGroup.getSelectedItem().toString();

编辑 2 - 请不要在不说明原因的情况下投反对票。以往任何时候都没有生产力

编辑 3这似乎是错误出现的地方。这个块包括类似 461 并用于将文本从文本微调器创建到单行:

public void updateLocalDesc(){

    AssetType = spnAssetType.getSelectedItem().toString();
    AssetGroup = spnAssetGroup.getSelectedItem().toString();

   if(spnAssetSubGroup.getSelectedItem() != null && !spnAssetSubGroup.getSelectedItem().toString().equals("")){
       AssetSubGroup = spnAssetSubGroup.getSelectedItem().toString();
       tbLongDescription.setText(AssetType + "-" + AssetGroup + "-" + AssetSubGroup);
   } else
   {
       tbLongDescription.setText(AssetType + "-" + AssetGroup);
   }

}
4

2 回答 2

0

我猜第 461 行是

mAssetType.add(c.getString(c.getColumnIndex("AssetType")));

并且 NullPointer 必须来自

c.getString()

检查 光标的android javadocs

如果未找到该列 c.getColumnIndex("AssetType") 将返回 -1 导致 c.getString() 抛出 NullPointerException。

您应该将该行分成 3 行并对其进行调试以准确找到问题。

于 2012-11-05T11:08:00.970 回答
0

我解决了。当我没有向数组传递任何内容时,第二个光标返回为 0。因此,当它尝试为描述区域设置文本时,它是 null 和空的,这样做:

public void updateLocalDesc(){

    if(spnAssetType.getSelectedItem() != null){
          AssetType = spnAssetType.getSelectedItem().toString(); 
    } else {
        AssetType = " ";
    }

    if(spnAssetGroup.getSelectedItem() != null){
        AssetGroup = spnAssetGroup.getSelectedItem().toString();
    } else {
        AssetGroup = " ";
    }
   AssetType = spnAssetType.getSelectedItem().toString();     


       if(spnAssetSubGroup.getSelectedItem() != null && !spnAssetSubGroup.getSelectedItem().toString().equals("")){
           AssetSubGroup = spnAssetSubGroup.getSelectedItem().toString();
           tbLongDescription.setText(AssetType + "-" + AssetGroup + "-" + AssetSubGroup);
       } else
       {
           tbLongDescription.setText(AssetType + "-" + AssetGroup);
       }

}

修复

于 2012-11-05T13:12:29.737 回答