我正在尝试在Android中创建一个自定义ListView
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
答案 0 :(得分:1)
更改getView()
方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){// Changed Here
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.bricklistitems, null);
}
Brick b = bricks.get(position);
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());// Changed Here
}
return v;
}
答案 1 :(得分:0)
这可能导致错误:
td.setText(b.getWidth());
这可能是调用带有资源ID的setText()。改为使用它(采用CharSequence的那个):
td.setText("" + b.getWidth());