我有一个片段,涉及根据其状态使用两个xml文件。因此,我希望设置一个视图“if”一个类是活动的“else”我希望它设置另一个视图。但是我遇到的错误是在下面的LOGCAT中显示的onCreateView中有一个空对象
public class MainInstagramActivity extends Fragment {
private InstagramSession mInstagramSession;
private Instagram mInstagram;
private ProgressBar mLoadingPb;
private GridView mGridView;
private static final String CLIENT_ID = "";
private static final String CLIENT_SECRET = "";
private static final String REDIRECT_URI = "";
@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 :(得分:3)
我认为onCreateView
在onViewCreated
之前执行。这就是你的引用为空的原因。
来自onViewCreated
的文档:
在onCreateView(LayoutInflater,ViewGroup, Bundle)已经返回
答案 1 :(得分:2)
在onViewCreated之前,控制转到onCreateView。这就是您收到错误的原因。在onCreateView中初始化instagram和instagramsession对象..它应该可以正常工作!
将这些行放在onCreateView方法
中mInstagram =新Instagram(getActivity(),CLIENT_ID,CLIENT_SECRET,REDIRECT_URI);
mInstagramSession = mInstagram.getSession();
答案 2 :(得分:0)
名为mInstagramSession的对象为null。你应该首先初始化InstagramSession。