2014-11-26 5 views
8

पर आक्रमण करने का प्रयास करें, मैं Fragments और कस्टम ListView एडेप्टर के लिए नौसिखिया हूं। क्या कोई मुझे हाथ दे सकता है?NullPointerException: वर्चुअल विधि 'int java.util.ArrayList.size()' को एक शून्य ऑब्जेक्ट संदर्भ

मुझे मिल गया है मेरी Fragment जहां मैं अपने ListView

public class RecordingListFragment extends Fragment implements OnItemClickListener { 

    ListView mListView; 
    TextView emptyView; 
    Button options, delete,edit; 
    ArrayList<String> recordings = null; 
    RecordingsListAdapter mAdapter; 

    public RecordingListFragment() { 
    } 

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

     options = (Button) rootView.findViewById(R.id.options); 
     edit = (Button) rootView.findViewById(R.id.edit); 
     delete = (Button) rootView.findViewById(R.id.delete); 
     mListView = (ListView) rootView.findViewById(R.id.list); 
     emptyView = (TextView) rootView.findViewById(R.id.empty); 


     mAdapter = new RecordingsListAdapter(this, recordings); 
     mListView.setAdapter(mAdapter); 
     mListView.setEmptyView(emptyView); 
     mListView.setOnItemClickListener(this); 

     return rootView; 
    } 
} 

और मेरे एक्सएमएल अपने कस्टम सूची आइटम है यह

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android1="http://schemas.android.com/apk/res/android" 
    android:id="@+id/recording_list_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_height="50dp" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:background="@drawable/title" > 

     <TextView 
      android1:id="@+id/title" 
      android1:layout_width="wrap_content" 
      android1:layout_height="50dp" 
      android1:layout_alignParentTop="true" 
      android1:layout_centerHorizontal="true" 
      android1:layout_marginStart="80dp" 
      android1:gravity="center" 
      android1:text="@string/app_name" 
      android1:textAlignment="center" 
      android1:textColor="#000000" 
      android1:textStyle="bold" /> 

    </RelativeLayout> 

    <FrameLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 

     <ListView 
      android:id="@+id/list" 
      android:animateLayoutChanges="true" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
     <!-- android:divider="@null"--> 

     <TextView 
      android:id="@+id/empty" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="@string/empty_list_text" 
      android:paddingLeft="30dp" 
      android:paddingRight="30dp" 
      android:paddingTop="180dp" /> 

    </FrameLayout> 
</LinearLayout> 

और मुझे मिल गया है के लिए

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:orientation="horizontal" > 

    <CheckedTextView 
     android:id="@+id/textView" 
     android:layout_width="fill_parent" 
     android:layout_height="?android:attr/listPreferredItemHeight" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:gravity="center_vertical" 
     android:checkMark="?android:attr/listChoiceIndicatorSingle" 
     android:paddingLeft="6dip" 
     android:paddingRight="6dip" 
     android:textColor="#000000" 
     /> 

</LinearLayout> 

और अंत में, मेरे Adapter

public class RecordingsListAdapter extends BaseAdapter { 
    Context ctx; 
    LayoutInflater lInflater; 
    ArrayList<String> recordings; 

    RecordingsListAdapter(Context context, ArrayList<String> products) { 
     ctx = context; 
     recordings = products; 
     lInflater = (LayoutInflater) ctx 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public RecordingsListAdapter(RecordingListFragment recordingListFragment, 
    ArrayList<String> recordings) { 
     // TODO Auto-generated constructor stub 
    } 


    @Override 
    public int getCount() { 
     return recordings.size(); 
    } 


    @Override 
    public Object getItem(int position) { 
     return recordings.get(position); 
    } 


    @Override 
    public long getItemId(int position) { 
     return position; 
    } 


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

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

     return view; 
    } 
} 

और जब मैं इस चलाने के लिए, मैं इस नल पॉइंटर

11-26 11:00:45.352: E/AndroidRuntime(10191): java.lang.RuntimeException: Unable to start activity ComponentInfo{d/recordings.RecordingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.access$800(ActivityThread.java:144) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.os.Handler.dispatchMessage(Handler.java:102) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.os.Looper.loop(Looper.java:135) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.main(ActivityThread.java:5221) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at java.lang.reflect.Method.invoke(Native Method) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at java.lang.reflect.Method.invoke(Method.java:372) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
11-26 11:00:45.352: E/AndroidRuntime(10191): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference 
11-26 11:00:45.352: E/AndroidRuntime(10191): at recordings.RecordingsListAdapter.getCount(RecordingsListAdapter.java:36) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.widget.ListView.setAdapter(ListView.java:487) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at recordings.RecordingListFragment.onCreateView(RecordingListFragment.java:47) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.Fragment.performCreateView(Fragment.java:2053) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.BackStackRecord.run(BackStackRecord.java:833) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.Activity.performStart(Activity.java:5948) 
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) 
11-26 11:00:45.352: E/AndroidRuntime(10191): ... 10 more 

अग्रिम धन्यवाद मिल !!!

+0

संभावित डुप्लिकेट [नल पॉइंटर अपवाद क्या है, और मैं इसे कैसे ठीक कर सकता हूं?] (Http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) – Selvin

+0

ऐरेलिस्ट रिकॉर्डिंग = शून्य; आपको इसे – Suvitruf

उत्तर

16

आपने रिकॉर्डिंग प्रारंभ करना चाहिए बनाते हैं। आप एडाप्टर शून्य

ArrayList<String> recordings = null; //You are passing this null 
6

बदलें

mAdapter = new RecordingsListAdapter(this, recordings); 

mAdapter = new RecordingsListAdapter(getActivity(), recordings); 

और यह भी करने के लिए सुनिश्चित करें कि recordings!=nullmAdapter = new RecordingsListAdapter(this, recordings); पर

+1

शुरू करना चाहिए, उसके पास संकलन समय त्रुटि नहीं है, इसलिए यह कोई समस्या नहीं है (वास्तव में वह एक अलग कोड है?) – Selvin

+0

@ सेल्विन ठीक है मेरे अपडेट देखें ... –

+0

मैंने आपके परिवर्तन को जोड़ा + मैं कुछ लोगों के साथ रिकॉर्डिंग स्थिर डेटा और मुझे यह त्रुटि मिलती है '11-26 11: 21: 47.125: ई/एंड्रॉइड रनटाइम (11596): java.lang.RuntimeException: गतिविधि कंपोनेंटइन्फो {xxxxx/recordings.RecordingsActivity} शुरू करने में असमर्थ: java.lang.NullPointerException: प्रयास एक शून्य ऑब्जेक्ट संदर्भ –

6

यह समस्या ArrayList चर के कारण तत्काल नहीं हो रही है। निम्नलिखित "रिकॉर्डिंग" चर घोषित करने की आवश्यकता है, जो समस्या को हल करना चाहिए;

ArrayList<String> recordings = new ArrayList<String>(); 

यह डिफ़ॉल्ट निर्माता कॉल करता है और रिकॉर्डिंग चर करने के लिए रिक्त स्ट्रिंग प्रदान करती है इतना है कि यह अब और अशक्त नहीं है।

संबंधित मुद्दे

 संबंधित मुद्दे