我已经制作了一个适配器,可以将imageview和textview中的图像和文本显示到listview中。
我尽了最大努力,但只要调用此活动,应用程序就会崩溃。
ImageAdapter.java:
public class ImageAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] filedescrptions;
private final Integer[] filenames;
public ImageAdapter(Activity context, String[] filedescrptions, Integer[] filenames) {
super(context, R.layout.list_item, filedescrptions);
this.context = context;
this.filedescrptions = filedescrptions;
this.filenames = filenames;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_item, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(filedescrptions[position]);
imageView.setImageResource(filenames[position]);
return rowView;
}
}
这是使用适配器的主要活动。
ImageActivity.java:
public class ImageActivity extends AppCompatActivity {
ListView list;
String[] descriptions = {"Abnormal menstrual bleeding",
"Adverse effects of HIV drugs",
"Antibiotics contraindicated during pregnancy",
"Antiemetic drugs"
};
Integer[] fileIds = {
R.drawable.abnromalmenstrualbleeding,
R.drawable.adverseeffectsofhivdrugs,
R.drawable.antibioticscontraindicatedduringpregnancy,
R.drawable.antiemeticdrugs
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageAdapter adapter = new ImageAdapter(ImageActivity.this, descriptions, fileIds);
list=(ListView)findViewById(R.id.listview_image);
list.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.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="@android:color/black"/>
</LinearLayout>
logcat 第38行引用list.setAdapter(adapter);
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vivek.pocketmedbook/com.vivek.pocketmedbook.ImageActivity}:java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.vivek.pocketmedbook.ImageActivity.onCreate(ImageActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:-1)
修改