1
votes

I have fragment i want to create a custom list view in it but when i send the parameters to adapter it's giving me error.

This is my code in Fragment..

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_list, container, false);

    TextView tv = (TextView) v.findViewById(R.id.textView1);

    CustomAdapter arrayAdapter = new CustomAdapter(getActivity(), R.layout.search_catagory_list, prgmImages, prgmNameList);
    lv = (ListView) v.findViewById(R.id.listView);

    lv.setAdapter(arrayAdapter);
    return v;
}

this is my MainHome.java

package com.example.yasee.bitescene.Fragments;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yasee.bitescene.R;
import com.example.yasee.bitescene.adapter.CustomAdapter;

import java.util.ArrayList;

public class MainHome extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;

ListView lv;
public static int [] prgmImages={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
public static String [] prgmNameList={"TRULY RANDOM","TEX MEX","BBQ","BRUNCH","SUSHI"};

private OnFragmentInteractionListener mListener;

public MainHome() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_list, container, false);

    TextView tv = (TextView) v.findViewById(R.id.textView1);

    CustomAdapter arrayAdapter = new CustomAdapter(getActivity(), R.layout.search_catagory_list, prgmImages, prgmNameList);
    lv = (ListView) v.findViewById(R.id.listView);

    lv.setAdapter(arrayAdapter);
    return v;
}

public static MainHome newInstance(String param1, String param2) {
    MainHome fragment = new MainHome();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}


public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

it show error like this..

enter image description here

this is my adapter code...

package com.example.yasee.bitescene.adapter;



import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yasee.bitescene.Fragments.MainHome;
import com.example.yasee.bitescene.R;
import com.example.yasee.bitescene.Search;

import java.util.ArrayList;

public class CustomAdapter extends BaseAdapter {
    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;

public CustomAdapter(Activity activity, int search_catagory_list, int[] prgmImages, String[] prgmNameList) {
    // TODO Auto-generated constructor stub
    result=prgmNameList;
    context=activity;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView tv;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.search_catagory_list, null);
    holder.tv= (TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

}

my error is...

06-13 07:31:00.399 3569-3569/com.example.yasee.bitescene E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.yasee.bitescene, PID: 3569 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yasee.bitescene/com.example.yasee.bitescene.Search}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.yasee.bitescene.Fragments.MainHome.onCreateView(MainHome.java:44) at android.app.Fragment.performCreateView(Fragment.java:1700) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) at android.app.BackStackRecord.run(BackStackRecord.java:684) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) at android.app.Activity.performStart(Activity.java:5240) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)  at android.app.ActivityThread.access$800(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5017)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  at dalvik.system.NativeStart.main(Native Method) 

3
what is the error message? point on the code to see the message and share it.AndroidMechanic - Viral Patel
well i'm too new in android when run my app it show... unfortunately stopped.Yaseen Ahmad
that's ok. but you should share the error you get for people here to be able to help you. what is the error message that you see in your log cat (Android Monitor window at the bottom in Android Studio) when you app crashes?AndroidMechanic - Viral Patel
i think are you talking about this Caused by: java.lang.NullPointerException at com.example.yasee.bitescene.Fragments.MainHome.onCreateView(MainHome.java:44)Yaseen Ahmad
yes, thats correct. share the full stack trace and what is at line#44 in your MainHome.java file?AndroidMechanic - Viral Patel

3 Answers

1
votes

lv appears to be null leading to the NullPointerException.

Make sure the id for for ListView is actually set to listView as it is expected in the line below. Also make sure it is inside the layout fragment_list

lv = (ListView) v.findViewById(R.id.listView);
0
votes

First create a new method in fragment and inflate like

LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View inflatedView2 = inflater.inflate(R.layout.activity_table_on_off_data, null);

create a new layout with listview and call it here 

TableReport1 = (ListView) inflatedView2.findViewById(R.id.dataonofflister);

and use the adapter like

tableReportofOnOffData = new TableReportofOnOffData(mActivity.getApplicationContext(), listData);
                TableReport1.setAdapter(null);
                TableReport1.setAdapter(tableReportofOnOffData);
0
votes

In Fragment

@

Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
   TableDisplayconditionView(date,getapplicationcontext());
   }

 create a new method outside of createview with parameters,

private void TableDisplayEventData(String date, Context context) {



    LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View inflatedView2 = inflater.inflate(R.layout.activity_event_details_data, null);
        Gallery.removeAllViews();
        Gallery.addView(inflatedView2);

        TableReport2 = (ListView) inflatedView2.findViewById(R.id.dataonofflister);

 tableReportofOnOffData = new TableReportofOnOffData(mActivity.getApplicationContext(), listData);
                TableReport1.setAdapter(null);
                TableReport1.setAdapter(tableReportofOnOffData);
}

The adapter looks like,

class TableReportofOnOffData extends BaseAdapter {

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

    if (view == null) {
        view = lInflater.inflate(R.layout.activity_table_on_off_data_text, parent, false);
    }

    ListerOnOffData p = getProduct(position);
    ListerOnOffData p2 = getProduct(position + 1);

    ((TextView) view.findViewById(R.id.date)).setText(p.getDate());
    ((TextView) view.findViewById(R.id.duration)).setText(p.getDuration());
    ((TextView) view.findViewById(R.id.powerfail)).setText(p.getPowerFail());
    ((TextView) view.findViewById(R.id.kwh)).setText(p.getKwh());

    return view;
}

}