Android Fragment でカスタム アダプターを使用すると問題が発生します。ここにコードがあります。しかし、このコードをアクティビティで使用する前は、すべて問題ありませんでした。Fragmentsの使用中にどこを間違えたのかわかりません。
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に何か問題があると思いますが、何...そしてなぜ単純なアクティビティではすべてがOKなのですか..
この問題を見つけてコードを操作しようとしましたが、まだ何もありません