自分の要素のリストを作成しようとしていますが、うまくいきません。アプリは起動しますが、この特定のビューに到達するとすぐにクラッシュします。誰かが何が悪いのか理解できることを願っています。2 つの Java クラスが表示され、エラー メッセージも表示されます。
これが最初のクラスです。それがフラグメントであり、アクティビティに変更したくないことに注意してください。
public class SektionerFragment extends SherlockFragment {
private ListView lv;
private SectionAdapter adapter;
private ArrayList<Section> fetch = new ArrayList<Section>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = inflater.inflate(R.layout.fragment_sektioner, container, false);
Section one = new Section(1,"Big1","Small1","Small1","Small1","Small1","Small1",2);
fetch.add(one);
lv = (ListView) getView().findViewById(R.id.SektionerList);
adapter = new SectionAdapter(getActivity(),
R.id.SektionerList,
fetch);
lv.setAdapter(adapter);
return contentView;
}
}
セカンドクラス:
public class SectionAdapter extends ArrayAdapter<Section>{
private ArrayList<Section> entries;
private Activity activity;
public SectionAdapter(Activity a, int textViewResourceId, ArrayList<Section> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView item1;
public TextView item2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_element_sektioner, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.sektion_sektionsnamn);
holder.item2 = (TextView) v.findViewById(R.id.sektion_programnamn);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Section custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getName());
holder.item2.setText(custom.getName());
}
return v;
}
}
エラーメッセージ:
06-29 17:16:34.059: D/AndroidRuntime(1241): Shutting down VM
06-29 17:16:34.059: W/dalvikvm(1241): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
06-29 17:16:34.069: E/AndroidRuntime(1241): FATAL EXCEPTION: main
06-29 17:16:34.069: E/AndroidRuntime(1241): java.lang.NullPointerException
06-29 17:16:34.069: E/AndroidRuntime(1241): at se.Appsolut.student.SektionerFragment.onCreateView(SektionerFragment.java:120)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.os.Handler.handleCallback(Handler.java:615)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.os.Handler.dispatchMessage(Handler.java:92)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.os.Looper.loop(Looper.java:137)
06-29 17:16:34.069: E/AndroidRuntime(1241): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-29 17:16:34.069: E/AndroidRuntime(1241): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 17:16:34.069: E/AndroidRuntime(1241): at java.lang.reflect.Method.invoke(Method.java:511)
06-29 17:16:34.069: E/AndroidRuntime(1241): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-29 17:16:34.069: E/AndroidRuntime(1241): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-29 17:16:34.069: E/AndroidRuntime(1241): at dalvik.system.NativeStart.main(Native Method)