我正在构建这个应用程序,我有一个parrent活动,到目前为止有6个片段,带有可滑动的标签,并且在每个片段中都有以编程方式添加的文本视图和少量图像视图,我想要做的是,我正在寻找一种只在片段可见的情况下加载东西的方法,我用调试器检查并注意到它很早就调用public void setMenuVisibility(boolean menuVisible) {
,我想我可以用那种方法做到。问题是它一旦开始加载视图就会崩溃,那么我该如何解决呢?问题可能与获取视图的正确上下文有关,不确定,这是我的片段:
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class Fragment10 extends Fragment {
LinearLayout layout;
ImageView iv;
String someUrl;
ArrayList<CharInfo> ci;
public Fragment10() {
// Empty constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment10, container, false);
layout = (LinearLayout) rootView.findViewById(R.id.layout);
iv = (ImageView) rootView.findViewById(R.id.iv);
Bundle b = this.getActivity().getIntent().getExtras();
ci = b.getParcelableArrayList("stuff");
someUrl = b.getString("someUrl");
return rootView;
}
@Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if (menuVisible) {
// View v = new View(getActivity());
TextView tv;
tv = new TextView(getActivity());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText(ci.get(0).name);
layout.addView(tv);
tv = new TextView(getActivity().getApplicationContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText(String.valueOf(ci.get(0).surname));
layout.addView(tv);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity()).build();
ImageLoader.getInstance().init(config);
String imgUrl = "http://" + someUrl + "someUrl.here" + regionUrl + "/" + ci.get(0).pic;
ImageLoader.getInstance().displayImage(imgUrl, iv);
}
}
}
另请注意我使用外部库进行图像查看。 这些是我得到的错误
08-23 08:12:19.977: E/AndroidRuntime(849): FATAL EXCEPTION: main
08-23 08:12:19.977: E/AndroidRuntime(849): java.lang.NullPointerException
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.View.<init>(View.java:3236)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.View.<init>(View.java:3281)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.widget.TextView.<init>(TextView.java:583)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.widget.TextView.<init>(TextView.java:578)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.widget.TextView.<init>(TextView.java:574)
08-23 08:12:19.977: E/AndroidRuntime(849): at com.stanga.wowarmory.Fragment1.setMenuVisibility(Fragment1.java:50)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:129)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.View.measure(View.java:15518)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.View.measure(View.java:15518)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.View.measure(View.java:15518)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-23 08:12:19.977: E/AndroidRuntime(849): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.View.measure(View.java:15518)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.Choreographer.doFrame(Choreographer.java:532)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.os.Handler.handleCallback(Handler.java:725)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.os.Looper.loop(Looper.java:137)
08-23 08:12:19.977: E/AndroidRuntime(849): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-23 08:12:19.977: E/AndroidRuntime(849): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 08:12:19.977: E/AndroidRuntime(849): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 08:12:19.977: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-23 08:12:19.977: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-23 08:12:19.977: E/AndroidRuntime(849): at dalvik.system.NativeStart.main(Native Method)
08-23 08:12:23.317: I/Process(849): Sending signal. PID: 849 SIG: 9
答案 0 :(得分:0)
试试这个
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
//Do your code which you did in setMenuVisibility() method
}
}