我在顯示數據庫中註冊的類別列表時遇到問題,在logcat中將這些錯誤返回給我。顯示在數據庫中註冊的項目時出錯!
的logcat:
04-26 10:43:18.475: ERROR/AndroidRuntime(2570): Uncaught handler: thread main exiting due to uncaught exception
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): java.lang.NullPointerException
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at br.com.codecreative.superlista.DisplayCategoria$CategoriaAdapter.getView(DisplayCategoria.java:112)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.ListView.makeAndAddView(ListView.java:1668)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.ListView.fillDown(ListView.java:637)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.ListView.fillFromTop(ListView.java:694)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.ListView.layoutChildren(ListView.java:1521)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.view.View.layout(View.java:6830)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.view.View.layout(View.java:6830)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.view.View.layout(View.java:6830)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.view.View.layout(View.java:6830)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.view.View.layout(View.java:6830)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.os.Looper.loop(Looper.java:123)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at java.lang.reflect.Method.invoke(Method.java:521)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-26 10:43:18.494: ERROR/AndroidRuntime(2570): at dalvik.system.NativeStart.main(Native Method)
代碼:
public class DisplayCategoria extends ListActivity {
MeuBanco dba;
CategoriaAdapter myAdapter;
private class MyCategoria {
public MyCategoria(String d) {
descricao = d;
}
public String descricao;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
dba = new MeuBanco(this);
dba.open();
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.listacategoria);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
ImageView btnInserelista = (ImageView) findViewById(R.id.insere);
btnInserelista.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent telaInsere = new Intent(DisplayCategoria.this,CadastraCategoria.class);
DisplayCategoria.this.startActivity(telaInsere);
}
});
super.onCreate(savedInstanceState);
myAdapter = new CategoriaAdapter(this);
this.setListAdapter(myAdapter);
}
private class CategoriaAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<MyCategoria> categorias;
public CategoriaAdapter(Context context){
mInflater = LayoutInflater.from(context);
categorias = new ArrayList<MyCategoria>();
getData();
}
private void getData() {
Cursor c = dba.getCategorias();
startManagingCursor(c);
if (c.moveToFirst()) {
do {
String descricao = c.getString(c.getColumnIndex(Constants.TITLE_NAME));
MyCategoria temp = new MyCategoria(descricao);
categorias.add(temp);
} while (c.moveToNext());
}
}
@Override
public int getCount() {
return categorias.size();
}
@Override
public MyCategoria getItem(int position) {
return categorias.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View v = convertView;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.categoriarow, null);
holder = new ViewHolder();
holder.mDescricao = (TextView) findViewById(R.id.desc_cat);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.mcategoria = getItem(position);
holder.mDescricao.setText(holder.mcategoria.descricao);
v.setTag(holder);
return v;
}
public class ViewHolder {
MyCategoria mcategoria;
TextView mDescricao;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backgroundlista">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:dividerHeight="1px"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
哪條線是112?根據堆棧跟蹤,它在getView()中的某處,但是你發佈的代碼並不長。順便說一下,這意味着它不是一個數據庫問題(至少不是直接)。 – 2011-04-26 14:05:52
第112行是這個holder.mDescricao.setText(holder.mcategoria.descricao);謝謝 ! – gangmobile 2011-04-26 15:07:19