我正在使用自定义适配器来显示列表视图。但是我得到了 NullPointerException。
public class moderationrequests extends Fragment {
public moderationrequests() {
}
private moderationrequestsadapter tableRowAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("ListFragment", "onCreate()");
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.v("ListFragment", "onActivityCreated().");
Log.v("ListsavedInstanceState", savedInstanceState == null ? "true"
: "false");
// Generate list View from ArrayList
displayListView();
}
public static final String ARG_SECTION_NUMBER = "section_number";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater
.inflate(R.layout.moderationrequests, container, false);
return v;
}
private void displayListView() {
String[] urlList = { "From Krishna on Business - 2 hours ago.",
"From Krishna on Business - 2 hours ago." };
tableRowAdapter = new moderationrequestsadapter(getActivity(),
R.layout.mod_req_list, urlList);
ListView listView = (ListView) getView().findViewById(R.id.list_view);
listView.setAdapter(tableRowAdapter);
}
}
适配器类
public class moderationrequestsadapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
private Button deleteButton = null;
public moderationrequestsadapter(Activity context, int itemResId,
String[] names) {
super(context, itemResId, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// LayoutInflater inflater = context.getLayoutInflater();
// View rowView = inflater.inflate(R.layout.mod_req_list, null, true);
View rowView = null;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.mod_req_list, null);
} else {
rowView = convertView;
}
deleteButton = (Button) rowView.findViewById(R.id.icon);
deleteButton.setTag(position);
TextView textView = (TextView) rowView.findViewById(R.id.rowId);
textView.setText(names[position]);
Log.d("views", rowView.toString());
return rowView;
}
}
日志猫
07-10 09:10:38.280: E/AndroidRuntime(3113): FATAL EXCEPTION: main
07-10 09:10:38.280: E/AndroidRuntime(3113): java.lang.NullPointerException
07-10 09:10:38.280: E/AndroidRuntime(3113): at com.main.udebate.moderationrequestsadapter.getView(moderationrequestsadapter.java:40)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.ListView.onMeasure(ListView.java:1158)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1389)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 09:10:38.280: E/AndroidRuntime(3113): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.os.Handler.handleCallback(Handler.java:725)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.os.Looper.loop(Looper.java:137)
07-10 09:10:38.280: E/AndroidRuntime(3113): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 09:10:38.280: E/AndroidRuntime(3113): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 09:10:38.280: E/AndroidRuntime(3113): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 09:10:38.280: E/AndroidRuntime(3113): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 09:10:38.280: E/AndroidRuntime(3113): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 09:10:38.280: E/AndroidRuntime(3113): at dalvik.system.NativeStart.main(Native Method)