这是一个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);
}
}
答案 0 :(得分:0)
我猜测第461行是
mAssetType.add(c.getString(c.getColumnIndex("AssetType")));
并且NullPointer必须来自
c.getString()
如果未找到该列,则c.getColumnIndex(“AssetType”)将返回-1,从而导致c.getString()抛出NullPointerException。
你应该将该行分成3行并调试它以准确找到问题。
答案 1 :(得分:0)
我解决了这个问题。当我没有将任何内容传递给数组时,第二个游标返回为0.所以当它尝试设置描述区域的文本时,它为空并且为空,所以这样做:
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);
}
}
修正了它