-1

i attemp bin CustomAdapter auf Fragment und Listview zu implementieren, aber ein Fehler Nullpointer in androidListView.setAdapter(androidListAdapter);Implementieren CustomAdapter zu Fragment Listview

Bisher zeigen versuchte ich CustomAdapter ohne Fragment, und es war ein Erfolg

Dies ist mein Fragment Code:

public class LaporanFragment extends Fragment { 
private ArrayAdapter<String> mLaporanAdapter; 
String androidListViewStrings[] = {"Android ListView Example", "Android Custom ListView Example", "Custom ListView Example", 
     "Android List Adapter", "Custom Adapter ListView", "ListView Tutorial", 
     "ListView with Image and Text", "Custom ListView Text and Image", "ListView Custom Tutorial"}; 

Integer image_id[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, 
     R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, 
     R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; 

public LaporanFragment(){ 
} 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_laporan, container, false); 
    LaporanAdapter androidListAdapter = new LaporanAdapter(getActivity(), image_id, androidListViewStrings); 
    ListView androidListView = (ListView) rootView.findViewById(R.id.list_view_laporan); 
    androidListView.setAdapter(androidListAdapter); **//Error nullPointerExeption on this line** 

    return rootView; 
}} 

Und das ist meine Gewohnheit Adapter Code:

public class LaporanAdapter extends ArrayAdapter { 
String[] androidListViewStrings; 
Integer[] imagesId; 
Context context; 

public LaporanAdapter(Activity context, Integer[] imagesId, String[] textListView) { 
    super(context, R.layout.list_item_laporan, textListView); 
    this.androidListViewStrings = textListView; 
    this.imagesId = imagesId; 
    this.context = context; 
} 

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 
    LayoutInflater layoutInflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View viewRow = layoutInflater.inflate(R.layout.list_item_laporan, null, 
      true); 
    TextView mtextView = (TextView) viewRow.findViewById(R.id.list_item_laporan_textview); 
    ImageView mimageView = (ImageView) viewRow.findViewById(R.id.image_view); 
    mtextView.setText(androidListViewStrings[i]); 
    mimageView.setImageResource(imagesId[i]); 
    return viewRow; 
}} 

UPDATE:

Logcat Ausgang:

FATAL EXCEPTION: main 
      Process: id.co.gloftech.pelaporandishub, PID: 4859 
      java.lang.RuntimeException: Unable to start activity ComponentInfo{id.co.gloftech.pelaporandishub/id.co.gloftech.pelaporandishub.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
       at android.app.ActivityThread.access$800(ActivityThread.java:151) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:135) 
       at android.app.ActivityThread.main(ActivityThread.java:5254) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:372) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
       at id.co.gloftech.pelaporandishub.LaporanFragment.onCreateView(LaporanFragment.java:85) 
       at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) 
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) 
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) 
       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682) 
       at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
       at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607) 
       at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181) 
       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236) 
       at android.app.Activity.performStart(Activity.java:6006) 
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) 
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  
       at android.app.ActivityThread.access$800(ActivityThread.java:151)  
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  
       at android.os.Handler.dispatchMessage(Handler.java:102)  
       at android.os.Looper.loop(Looper.java:135)  
       at android.app.ActivityThread.main(ActivityThread.java:5254)  
       at java.lang.reflect.Method.invoke(Native Method)  
       at java.lang.reflect.Method.invoke(Method.java:372) 

Und die fragment_laporan.xml

<FrameLayout 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$LaporanFragment"> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/list_view_forecast"></ListView> 

+0

Zeigen Sie den logcat Ausgang und 'fragment_laporan.xml' Code. –

+0

@KNeerajLal danke, ich habe meine Frage aktualisiert –

Antwort

0

Du hast die ListView id falsch. So wird rootView.findViewById(R.id.list_view_laporan)null zurückgeben und zu einem NPE führen.

ändern,

ListView androidListView = (ListView) rootView.findViewById(R.id.list_view_laporan); 

zu,

ListView androidListView = (ListView) rootView.findViewById(R.id.list_view_forecast); 
+1

Danke, es funktioniert, ich habe die ID falsch –