所以我正在开发一个Android应用程序并且我有一个gridview,在这个gridview中我想设置一个onClickListener,这样无论按下哪个项目,它都会启动一个新片段。根据gridview中项目的位置,新片段的内容将有所不同。但是当我尝试启动这个片段时,我的应用程序由于空指针异常而崩溃。我不知道如何阻止这一点,你们有什么想法吗?
以下是包含gridview的片段中onCreate函数的代码:
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_tea_selections, container, false);
getActivity().setTitle("Select a Tea!");
// animation when enter home page
rootView.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.image_click));
//download the URL's asynchronously (put the info in the teaInfo object)
try {
teaInfo = new GetTeaInfoTask().execute(new ApiConnector()).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//make the gridview, set its adapter
GridView gridView = (GridView)rootView.findViewById(R.id.grid_view_tea_selections);
GridViewCustomAdapter gvAdapter = new GridViewCustomAdapter(getActivity(), teaInfo.imageURLs, teaInfo.teaNames);
gridView.setAdapter(gvAdapter);
//make the on click listeners
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Fragment newFragment;
switch(position) {
default:
newFragment = new TeaViewFragment();
Bundle teaViewArgs = new Bundle();
teaViewArgs.putString("teaName", teaInfo.teaNames.get(position));
teaViewArgs.putString("teaImgURL", teaInfo.imageURLs.get(position));
teaViewArgs.putString("teaDesc", teaInfo.teaDescriptions.get(position));
break;
}
if (newFragment != null) {
flipCard(newFragment);
}
}
private void flipCard(Fragment newFragment){
mShowingBack = true;
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().setCustomAnimations(
R.anim.card_flip_right_in, R.anim.card_flip_right_out,
R.anim.card_flip_left_in, R.anim.card_flip_left_out).replace(R.id.frame_container, newFragment)
.addToBackStack(null).commit(); // create new fragment and allow user to go back to previous fragment
}
});
return rootView;
}
根据logcat,空行指针异常发生在这一行:newFragment = new TeaViewFragment();
编辑: 这是teaViewFragment代码:
public class TeaViewFragment extends Fragment {
private String teaDesc;
private String teaName;
private String teaImgUrl;
private ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity())
.threadPriority(Thread.NORM_PRIORITY-2)
.denyCacheImageMultipleSizesInMemory()
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.diskCacheSize(10*1024*1024)
.tasksProcessingOrder(QueueProcessingType.LIFO)
.writeDebugLogs()
.build();
public ImageLoader imageLoader;
private DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_loading)
.showImageForEmptyUri(R.drawable.not_found_exclamation)
.showImageOnFail(R.drawable.not_found_exclamation)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tea_view, container, false);
//private members
//Get the information about the tea from the MySelections fragment
teaDesc = getArguments().getString("teaDesc");
teaName = getArguments().getString("teaName");
teaImgUrl = getArguments().getString("teaImgUrl");
getActivity().setTitle(teaName); //the title of the action bar is the name of the tea
ImageView teaPic = (ImageView) getView().findViewById(R.id.teaViewImage);
TextView teaDescView = (TextView) getView().findViewById(R.id.teaViewDesc);
//set the image loader config, display the image and set the description
imageLoader.init(config);
imageLoader.displayImage(teaImgUrl, teaPic, options);
teaDescView.setText(teaDesc);
return rootView;
}
}
以下是发生null ptr异常的logcat:
08-07 21:13:45.911: E/AndroidRuntime(6706): FATAL EXCEPTION: main
08-07 21:13:45.911: E/AndroidRuntime(6706): java.lang.NullPointerException
08-07 21:13:45.911: E/AndroidRuntime(6706): at com.nostra13.universalimageloader.core.ImageLoaderConfiguration$Builder.<init>(ImageLoaderConfiguration.java:196)
08-07 21:13:45.911: E/AndroidRuntime(6706): at models.TeaViewFragment.<init>(TeaViewFragment.java:25)
08-07 21:13:45.911: E/AndroidRuntime(6706): at com.example.t_danbubbletea.MySelections$1.onItemClick(MySelections.java:67)
08-07 21:13:45.911: E/AndroidRuntime(6706): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-07 21:13:45.911: E/AndroidRuntime(6706): at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
08-07 21:13:45.911: E/AndroidRuntime(6706): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
08-07 21:13:45.911: E/AndroidRuntime(6706): at android.widget.AbsListView$1.run(AbsListView.java:3529)
08-07 21:13:45.911: E/AndroidRuntime(6706): at android.os.Handler.handleCallback(Handler.java:615)
08-07 21:13:45.911: E/AndroidRuntime(6706): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 21:13:45.911: E/AndroidRuntime(6706): at android.os.Looper.loop(Looper.java:137)
08-07 21:13:45.911: E/AndroidRuntime(6706): at android.app.ActivityThread.main(ActivityThread.java:5003)
08-07 21:13:45.911: E/AndroidRuntime(6706): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 21:13:45.911: E/AndroidRuntime(6706): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 21:13:45.911: E/AndroidRuntime(6706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-07 21:13:45.911: E/AndroidRuntime(6706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
08-07 21:13:45.911: E/AndroidRuntime(6706): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
试试这个..
改变这个..
ImageView teaPic = (ImageView) getView().findViewById(R.id.teaViewImage);
TextView teaDescView = (TextView) getView().findViewById(R.id.teaViewDesc);
到
ImageView teaPic = (ImageView) rootView.findViewById(R.id.teaViewImage);
TextView teaDescView = (TextView) rootView.findViewById(R.id.teaViewDesc);
答案 1 :(得分:0)
问题在于:
private ImageLoaderConfiguration config =
new ImageLoaderConfiguration.Builder(getActivity())...
您无法在那里创建ImageLoaderConfiguration
。赋值将在构造时发生,此时getActivity()
返回null。您必须等到片段附加到Activity
,因此请在onAttach()
或更高版本中执行此代码。