我開發一個Android應用程序在那裏我得到這個代碼從一個例子來充當列表。此頁面是3頁ViewPager的片段活動部分。代碼:NullPointerException異常與碎片和自定義列表
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class OverviewFragment extends Fragment {
private ViewGroup mContainerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_overview, container, false);
// Initialize
mContainerView = (ViewGroup)getActivity().findViewById(R.id.container);
// Add some items
addItem();
addItem();
addItem();
addItem();
return rootView;
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu, null);
getActivity().getMenuInflater().inflate(R.menu.activity_overviewfragment, menu);
return true;
}
private void addItem() {
// Instantiate a new "row" view.
final ViewGroup newView = (ViewGroup) LayoutInflater.from(getActivity()).inflate(
R.layout.upcoming_list_item, mContainerView, false);
// Set the text in the new row to a random country.
((TextView) newView.findViewById(android.R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);
// Set a click listener for the "X" button in the row that will remove the row.
/**
newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Remove the row from its parent (the container view).
// Because mContainerView has android:animateLayoutChanges set to true,
// this removal is automatically animated.
mContainerView.removeView(newView);
// If there are no rows remaining, show the empty view.
if (mContainerView.getChildCount() == 0) {
findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
}
}
});
*/
// Because mContainerView has android:animateLayoutChanges set to true,
// adding this view is automatically animated.
mContainerView.addView(newView, 0);
}
private static final String[] UPCOMING = new String[] {
"Apartmen loan -- 200 $", "Car fix invoice -- 300 $", "Internet bill -- 70$", "dinner -- 20$"
};
}
錯誤:
06-23 13:57:44.440: E/AndroidRuntime(6200): FATAL EXCEPTION: main
06-23 13:57:44.440: E/AndroidRuntime(6200): Process: com.acceleratedcode.apps.myApp, PID: 6200
06-23 13:57:44.440: E/AndroidRuntime(6200): java.lang.NullPointerException
06-23 13:57:44.440: E/AndroidRuntime(6200): at com.acceleratedcode.apps.myApp.OverviewFragment.addItem(OverviewFragment.java:47)
06-23 13:57:44.440: E/AndroidRuntime(6200): at com.acceleratedcode.apps.myApp.OverviewFragment.onCreateView(OverviewFragment.java:27)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
錯誤在於AddItem方法(很肯定)
XML中的某處:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<LinearLayout android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
到底在哪您OverviewFragment線47? – tknell
我怎樣才能找到沒有手動計數?我有點新的日食:) – user3680475
http://eclipse.dzone.com/articles/line-numbers-eclipse – tknell