我是Android应用程序编程的初学者。
当我使用带有ListAdapter的片段时,我遇到了错误。
这是源代码,
public class RegReceiptFragment extends Fragment {
private Context mContext;
private ListView mRegisteredPLUList;
private List<String> mDescTestArr;
private RegReceiptFragmentListAdapter mRegReceiptListAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.regmain_receipt_layout, container, false);
mRegisteredPLUList = (ListView) getActivity().findViewById(R.id.reason_list);
mDescTestArr = new ArrayList<String>();
testPutListItem(null);
return myFragmentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext = getActivity().getBaseContext();
mRegReceiptListAdapter = new RegReceiptFragmentListAdapter(mContext, R.layout.common_list_item, mDescTestArr);
if (mRegReceiptListAdapter == null)
Log.d("masstest", "listview is null!!!");
else
mRegisteredPLUList.setAdapter(mRegReceiptListAdapter);
}
private void testPutListItem(String value) {
if (value == null) {
mDescTestArr.add("BAD!!");
mDescTestArr.add("HAHAHAHA!!");
mDescTestArr.add("GEE GEE!!");
} else {
mDescTestArr.add(value);
}
}
}
public class RegReceiptFragmentListAdapter extends ArrayAdapter<String> {
private Context mContext;
private final int mResourceId;
private List<String> mTestDescArr;
public RegReceiptFragmentListAdapter(Context context,
int textViewResourceId, List<String> arry) {
super(context, textViewResourceId, arry);
mContext = context;
mResourceId = textViewResourceId;
mTestDescArr = arry;
}
public View getView(int position, View convertView, ViewGroup parent) {
ReasonListHolder holder;
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(mResourceId, parent, false);
holder = new ReasonListHolder();
holder.mDescTitle = (TextView) row.findViewById(R.id.list_description);
holder.mQuantity = (TextView) row.findViewById(R.id.list_quantity);
holder.mPrice = (TextView) row.findViewById(R.id.list_price);
row.setTag(holder);
} else {
holder = (ReasonListHolder) row.getTag();
}
holder.mDescTitle.setText(mTestDescArr.get(position));
holder.mQuantity.setText(String.valueOf(position));
holder.mPrice.setText(String.valueOf(position));
return (row);
}
class ReasonListHolder {
TextView mQuantity;
TextView mDescTitle;
TextView mPrice;
}
}
,错误代码是下一个
01-12 09:16:01.950: W/dalvikvm(26465): threadid=1: thread exiting with uncaught exception (group=0x41884700)
01-12 09:16:01.950: E/AndroidRuntime(26465): FATAL EXCEPTION: main
01-12 09:16:01.950: E/AndroidRuntime(26465): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sam4s.sample.fragment.test004/com.sam4s.sample.fragment.test004.MainActivity}: java.lang.NullPointerException
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.os.Looper.loop(Looper.java:137)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-12 09:16:01.950: E/AndroidRuntime(26465): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 09:16:01.950: E/AndroidRuntime(26465): at java.lang.reflect.Method.invoke(Method.java:525)
01-12 09:16:01.950: E/AndroidRuntime(26465): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-12 09:16:01.950: E/AndroidRuntime(26465): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-12 09:16:01.950: E/AndroidRuntime(26465): at dalvik.system.NativeStart.main(Native Method)
01-12 09:16:01.950: E/AndroidRuntime(26465): Caused by: java.lang.NullPointerException
01-12 09:16:01.950: E/AndroidRuntime(26465): at com.sam4s.sample.fragment.test004.RegReceiptFragment.onActivityCreated(RegReceiptFragment.java:60)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1794)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:967)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:544)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.app.Activity.performStart(Activity.java:5143)
01-12 09:16:01.950: E/AndroidRuntime(26465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
01-12 09:16:01.950: E/AndroidRuntime(26465): ... 11 more
活动是下一个
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
}
}
}
和activity_main.xml是
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/fragment_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.sam4s.sample.fragment.test004.RegPLUFragment" />
</FrameLayout>
答案 0 :(得分:0)
我找到了原因。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
mRegisteredPLUList = (ListView) getActivity().findViewById(R.id.reason_list);
...
}
此代码应移至
@Override
public void onActivityCreated(Bundle savedInstanceState) {
....
mRegisteredPLUList = (ListView) getActivity().findViewById(R.id.reason_list);
....
}
这个错误来自我的编码而不了解片段生命周期。
谢谢!