2012-12-27 170 views
0

我在使用Android Fragment中的自定義適配器時遇到了一些問題。 這裏是代碼。但是在我使用這段代碼之前,一切都很好。我無法找到我在使用Fragments時犯的錯誤。android片段自定義適配器

package com.testapp.test.fragments; 

import org.holoeverywhere.LayoutInflater; 
import org.holoeverywhere.app.Fragment; 
import com.testapp.test.R; 

import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 

public class LawDocFragment extends Fragment{ 

private static LawDocFragment instance; 

public static LawDocFragment getInstance() { 
    if (LawDocFragment.instance == null) { 
     return new LawDocFragment(); 
    } 
    return LawDocFragment.instance; 
} 

public LawDocFragment() { 
    LawDocFragment.instance = this; 
} 

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

String[] zakonmenubig; 
String[] zakonmenusmall; 
Integer[] zakonmenuicon = { R.drawable.ic_menu_zakonu_pravila, 
          R.drawable.ic_menu_zakonu_strafu, 
          R.drawable.ic_menu_zakonu_doc, 
          R.drawable.ic_menu_zakonu_u77, 
          R.drawable.ic_menu_zakonu_u33, 
          R.drawable.ic_menu_zakonu_u400, 
          R.drawable.ic_menu_zakonu_p1103}; 

myAdapter zakonAdapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View LawDoc = inflater.inflate(R.layout.zakonbaza);  
    ListView zakonmenulist = (ListView)LawDoc.findViewById(R.id.zakonmenulist);  
    zakonAdapter = new myAdapter();  
    zakonmenulist.setAdapter(zakonAdapter); 

    zakonmenubig = getResources().getStringArray(R.array.zakonmenubig); 
    zakonmenusmall = getResources().getStringArray(R.array.zakonmenusmall); 

    return LawDoc; 
} 

//start myAdapter 
public class myAdapter extends BaseAdapter { 
    private LayoutInflater mLayoutInflater; 

    public myAdapter(Context ctx) { 
     mLayoutInflater = LayoutInflater.from(ctx); 
    } 

    public int getCount() { 
     return zakonmenubig.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

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

    public String getString(int position) { 
     return zakonmenubig[position] + " (" + zakonmenusmall[position] + ")"; 
    } 

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

     if (convertView == null) 
      convertView = mLayoutInflater.inflate(R.layout.list_zakonbaza, null); 

     ImageView image = (ImageView) convertView.findViewById(R.id.icon); 
     image.setImageResource(zakonmenuicon[position]); 

     TextView sign = (TextView) convertView.findViewById(R.id.zakonmenuitem); 
     sign.setText(zakonmenubig[position]); 

     TextView date = (TextView) convertView.findViewById(R.id.zakonmenuitemsmall); 
     date.setText(zakonmenusmall[position]); 
     return convertView; 
    } 
} // end myAdapter 

} 

現在的Eclipse顯示我的錯誤 「的構造LawDocFragment.myAdapter()是未定義」 看齊

zakonAdapter = new myAdapter(); 

我是否會改變

zakonAdapter = new myAdapter(getActivity()); 

我會得到這個異常

12-27 00:39:25.297: E/AndroidRuntime(652): FATAL EXCEPTION: main 
12-27 00:39:25.297: E/AndroidRuntime(652): java.lang.NullPointerException 
12-27 00:39:25.297: E/AndroidRuntime(652): at com.testapp.test.fragments.fragments.LawDocFragment$myAdapter.getCount(LawDocFragment.java:72) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.widget.ListView.setAdapter(ListView.java:436) 
12-27 00:39:25.297: E/AndroidRuntime(652): at com.testapp.test.fragments.fragments.LawDocFragment.onCreateView(LawDocFragment.java:55) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.support.v4.app._HoloFragment.onCreateView(_HoloFragment.java:177) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.os.Handler.handleCallback(Handler.java:587) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.os.Handler.dispatchMessage(Handler.java:92) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.os.Looper.loop(Looper.java:123) 
12-27 00:39:25.297: E/AndroidRuntime(652): at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-27 00:39:25.297: E/AndroidRuntime(652): at java.lang.reflect.Method.invokeNative(Native Method) 
12-27 00:39:25.297: E/AndroidRuntime(652): at java.lang.reflect.Method.invoke(Method.java:521) 
12-27 00:39:25.297: E/AndroidRuntime(652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-27 00:39:25.297: E/AndroidRuntime(652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-27 00:39:25.297: E/AndroidRuntime(652): at dalvik.system.NativeStart.main(Native Method)  

線「LawDocFragment.java:72」我有這樣的代碼

return zakonmenubig.length; 

和行「LawDocFragment.java:55」我有

zakonmenulist.setAdapter(zakonAdapter); 

我覺得有什麼不對類myAdapter但什麼。 ..爲什麼在簡單的活動一切正常..

我發現這個issue,並嘗試與代碼,但仍然沒有工作

回答

1

確定這裏的問題是

  1. 由於在您的適配器類中沒有類似的構造函數,所以具有空白構造函數的話顯示錯誤是正確的。因此,要麼創建一個沒有參數的構造函數(壞主意),要麼傳入上下文。

  2. 你的空指針的發生是因爲

    zakonmenubig

沒有定義然而,當你正在創建的適配器,而不是你的適配器像在此之前創建的那些元素:

zakonmenubig = getResources().getStringArray(R.array.zakonmenubig); 
zakonmenusmall = getResources().getStringArray(R.array.zakonmenusmall); 

ListView zakonmenulist = (ListView)LawDoc.findViewById(R.id.zakonmenulist);  
zakonAdapter = new myAdapter();  
zakonmenulist.setAdapter(zakonAdapter); 

現在你的zakonmenubig將被初始化。

+0

** mikeswright49 **非常感謝你,現在一切工作:) – Iar0slav