我的应用程序构建,但它崩溃,我不知道为什么。 基本上我在MainActivity中实现BaseAdapter,稍后我将把它放在另一个类
中这是MainActivity.java
public class MainActivity extends Activity {
ListView mListView;
String[] mTitle={"item1","item2"};
String[] mDetail={"info1","info2"};
int[] mImage ={R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView=(ListView)findViewById(R.id.list);
mListView.setAdapter(new dataListAdapter(mTitle, mDetail, mImage, this));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
context = null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3, Context ctx) {
Title = text;
Detail = text1;
imge = text3;
context = ctx;
}
public final Context context;
public dataListAdapter(Context mContext){
this.context = mContext;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView titleView, detailView;
ImageView imageView;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.custom, parent,false);
titleView = (TextView) convertView.findViewById(R.id.title);
detailView = (TextView) convertView.findViewById(R.id.detail);
imageView = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(R.id.title, titleView);
convertView.setTag(R.id.detail, detailView);
convertView.setTag(R.id.image, imageView);
}else{
titleView = (TextView) convertView.getTag(R.id.title);
detailView = (TextView) convertView.getTag(R.id.detail);
imageView = (ImageView) convertView.getTag(R.id.image);
}
titleView.setText(Title[position]);
detailView.setText(Detail[position]);
imageView.setImageDrawable(context.getResources().getDrawable(imge[position]));
return convertView;
}
}
logcat的:
01-10 21:20:43.290 26874-26874/com.robigroza.justlisttest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.robigroza.justlisttest.MainActivity$dataListAdapter.getView(MainActivity.java:91)
at android.widget.AbsListView.obtainView(AbsListView.java:2465)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
at android.widget.ListView.onMeasure(ListView.java:1162)
at android.view.View.measure(View.java:15371)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)
at android.view.View.measure(View.java:15371)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2011)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1250)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1425)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4674)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
这是custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="255dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#339966"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="video1"
android:textColor="#606060" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
这是main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
答案 0 :(得分:1)
您的custom.xml
应包含2 TextViews
(标题和详细信息)和ImageView
(图片)。
E.g。像这样的东西:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"/>
</LinearLayout>
答案 1 :(得分:0)
试试这个。
Imagview.setImageResource (img [position]);