最近将一个活动更改为一个片段之后我已经使用Layoutinflator来获取片段的用户界面但是我遇到了一个运行时错误,该错误显示在下面的LOGCAT中我该如何解决这个问题?
public class MainInstagramActivity extends Fragment {
private InstagramSession mInstagramSession;
private Instagram mInstagram;
private ProgressBar mLoadingPb;
private GridView mGridView;
private static final String CLIENT_ID = "83549f9eb76f4a5b90daf6e4e14da107";
private static final String CLIENT_SECRET = "6df26b0c8f664323a07126bfe8511651";
private static final String REDIRECT_URI = "http://www.yahoo.com";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
if (mInstagramSession.isActive()) {
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.activity_user_instagram, null);
return view;
}
else {
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.activity_main_instagram, null);
return view;
}
}
@Override
public void onViewCreated(View container, Bundle savedInstanceState) {
super.onViewCreated(container, savedInstanceState);
mInstagram = new Instagram(getActivity(), CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
mInstagramSession = mInstagram.getSession();
if (mInstagramSession.isActive()) {
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.activity_user_instagram, null);
InstagramUser instagramUser = mInstagramSession.getUser();
mLoadingPb = (ProgressBar)container.findViewById(R.id.pb_loading);
mGridView = (GridView)container.findViewById(R.id.gridView);
((TextView) container.findViewById(R.id.tv_name)).setText(instagramUser.fullName);
((TextView) container.findViewById(R.id.tv_username)).setText(instagramUser.username);
((Button) container.findViewById(R.id.btn_logout)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mInstagramSession.reset();
startActivity(new Intent(getActivity(), MainInstagramActivity.class));
getActivity().getFragmentManager().popBackStack();
}
});
ImageView userIv = (ImageView) container.findViewById(R.id.iv_user);
DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_user)
.showImageForEmptyUri(R.drawable.ic_user)
.showImageOnFail(R.drawable.ic_user)
.cacheInMemory(true)
.cacheOnDisc(false)
.considerExifParams(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity())
.writeDebugLogs()
.defaultDisplayImageOptions(displayOptions)
.build();
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
AnimateFirstDisplayListener animate = new AnimateFirstDisplayListener();
imageLoader.displayImage(instagramUser.profilPicture, userIv, animate);
new DownloadTask().execute();
} else {
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.activity_main_instagram, null);
((Button) container.findViewById(R.id.btn_connect)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mInstagram.authorize(mAuthListener);
}
});
}
}
logcat的
09-22 10:41:31.065 986-986/com.virtualinsomniac.annaakana E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.virtualinsomniac.annaakana, PID: 986
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean instagramlibrary.InstagramSession.isActive()' on a null object reference
at com.virtualinsomniac.annaakana.Instagram.MainInstagramActivity.onCreateView(MainInstagramActivity.java:63)
at android.app.Fragment.performCreateView(Fragment.java:1704)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1063)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1448)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
答案 0 :(得分:0)
这会返回一个视图,因此您可以返回该视图并在您的活动中对其进行充气,而不是让您的片段中的视图膨胀。