0

I have made an adapter to display images and text from imageview and textview into listview.

I tried my best but the application crashes whenever this activity is called.

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;

  }
}

This is the main activity where the adapter is used.

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 38th line referes to 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) 

1 Answers1

-1

Modify

  1. ImageAdapter extends BaseAdapter
  2. In getView method View rowView= inflater.inflate(R.layout.list_item, null, false)