2013-08-23 79 views
1

我正在構建此應用程序,其中有一個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

一般來說,你做getViews/findviewbyid。然後在onViewCreated中執行setAdapter ...所有需要視圖的東西。在onActivity創建的時候,你可以做一些需要將活動初始化爲GetActivity()方法的東西。像傳遞給片段的包/參數之類的東西可以在onCreate方法中。 –

+0

但我可以在setMenuVisibility方法中做這些事情嗎? –

+0

你當然可以做你想做的事;但如果我是你,我不會。有兩個原因:首先,該方法中的GetActivity()可能會使應用程序崩潰,其次,如果您是調用該方法來隱藏/顯示該應用程序的人會以某種方式解決問題,否則它將無法正常工作,因爲您無法控制你的用戶界面發生了什麼^^「 –

回答

0

錯誤嘗試在onCreateView方法本

@Override 
    public void setUserVisibleHint(boolean isVisibleToUser) { 
     super.setUserVisibleHint(isVisibleToUser); 
     if(isVisibleToUser){ 
      //Do your code which you did in setMenuVisibility() method 
     } 
    } 
相關問題