我正在尝试ListView
在 Android 中创建一个自定义 ArrayAdapter,它显示来自我创建的名为 Brick 的类的数据。我的主要活动调用了一个返回 ArrayList 的方法。我不知道我做错了什么,LogCat 和 Debugger 并没有告诉我太多。我认为这是我的错误,因为是 Android 新手。当我启动我的 ListActivity 时,应用程序强制关闭。我已经尝试了多个教程,但进展缓慢。
这是我的自定义适配器:
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class BrickListAdapter extends ArrayAdapter<Brick> {
private ArrayList<Brick> bricks;
private Context mContext;
public BrickListAdapter(Context context, int textViewResourceId, ArrayList<Brick> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.bricks = objects;
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v != null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.bricklistitems, null);
Brick b = bricks.get(position);
if(b != null){
TextView tn = (TextView) v.findViewById(R.id.listitemname);
TextView td = (TextView) v.findViewById(R.id.listitemdesc);
if(tn !=null){
tn.setText(b.getName());
}
if(td != null){
td.setText(b.getWidth());
}
}
}
return v;
}
}
这是我的 ListActivity:
public class ListActivity extends Activity {
private static ArrayList<Brick> a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
a = new BrickTypes().getBrickTypes();
BrickListAdapter adapter = new BrickListAdapter(this, R.layout.bricklistitems, a);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
}
好的,这里是 LogCat。我看到我收到了 NullPointerException,但我不明白是从哪里来的。
10-31 03:55:22.772: E/AndroidRuntime(4799): FATAL EXCEPTION: main
10-31 03:55:22.772: E/AndroidRuntime(4799): java.lang.NullPointerException
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.widget.AbsListView.obtainView(AbsListView.java:2269)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.widget.ListView.onMeasure(ListView.java:1156)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.View.measure(View.java:15172)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.View.measure(View.java:15172)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.View.measure(View.java:15172)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.View.measure(View.java:15172)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-31 03:55:22.772: E/AndroidRuntime(4799): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.View.measure(View.java:15172)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.Choreographer.doFrame(Choreographer.java:525)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.os.Handler.handleCallback(Handler.java:615)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.os.Handler.dispatchMessage(Handler.java:92)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.os.Looper.loop(Looper.java:137)
10-31 03:55:22.772: E/AndroidRuntime(4799): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-31 03:55:22.772: E/AndroidRuntime(4799): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 03:55:22.772: E/AndroidRuntime(4799): at java.lang.reflect.Method.invoke(Method.java:511)
10-31 03:55:22.772: E/AndroidRuntime(4799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-31 03:55:22.772: E/AndroidRuntime(4799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 03:55:22.772: E/AndroidRuntime(4799): at dalvik.system.NativeStart.main(Native Method)
10-31 03:55:22.801: W/ActivityManager(157): Force finishing activity net.stewartservices.brickconverter/.ListActivity