[英]NullPointerException with fragments and custom list
我正在开发一个Android应用程序,我从一个示例中获取了此代码以用作列表。 此页面是3页ViewPager的片段活动部分。 码:
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>
您的问题很可能在以下行中出现:
((TextView) newView.findViewById(android.R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);
更改为:(假设您的布局中的TextView
的ID为“ text1”)
((TextView) newView.findViewById(R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);
您所引用的ID已被Android占用,并且(可能)在布局文件中找不到。
在此之上:
尝试对您的“ newView”使用View而不是ViewGroup
。 并为您的“ mContainerView”使用LinearLayout
而不是ViewGroup
。
android.R.id.text1不能在以下代码中直接使用。
((TextView) newView.findViewById(android.R.id.text1)).setText( UPCOMING[(int) (Math.random() * UPCOMING.length)]);
你应该创建一个textview然后使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.