2013-04-26 67 views
0

我有以下代碼:getView()的片段引起異常被拋出,不知道爲什麼

package com.example.mytestapp; 


public class ExpenseSelection extends Fragment { 

    public static final ExpenseSelection newInstance() { 
     ExpenseSelection f = new ExpenseSelection(); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.activity_expense_selection, container, false); 

     return v; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 


     ViewGroup view = (ViewGroup)getView().findViewById(R.id.expensemainlayout); 
     ExpenseIconGridGenerate.GenerateIcons(getActivity(), view); 
    } 
} 

備份以下XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:id="@+id/expensemainlayout" 
    tools:context=".ExpenseSelection" > 

    <LinearLayout 
     android:id="@+id/ExpenseIconsLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:height="10dp" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hello, I am a Button" /> 
    </LinearLayout> 

</RelativeLayout> 

就行了 'getView' I」 m得到一個異常拋出。

我想要做的就是在oncreate方法中獲取視圖,我認爲這是我在構建表單控件時應該做的事情?否則,我可以簡單地使用onCreateView中的視圖來生成活動視圖,但從設計的角度來看似乎是錯誤的。

這裏是日誌文件時,該行執行:由R.layout.expensemainlayout

> 04-26 22:47:12.400: D/AndroidRuntime(4036): Shutting down VM 04-26 
> 22:47:12.400: W/dalvikvm(4036): threadid=1: thread exiting with 
> uncaught exception (group=0x41ebc930) 04-26 22:47:12.440: 
> E/AndroidRuntime(4036): FATAL EXCEPTION: main 04-26 22:47:12.440: 
> E/AndroidRuntime(4036): java.lang.NullPointerException 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> com.example.mytestapp.ExpenseSelection.onCreate(ExpenseSelection.java:34) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.support.v4.app.Fragment.performCreate(Fragment.java:1437) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:801) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.support.v4.view.ViewPager.populate(ViewPager.java:1009) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.support.v4.view.ViewPager.populate(ViewPager.java:880) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440: 
> E/AndroidRuntime(4036): at 
> android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440: 
> E/AndroidRuntime(4036): at 
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440: 
> E/AndroidRuntime(4036): at 
> android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440: 
> E/AndroidRuntime(4036): at 
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440: 
> E/AndroidRuntime(4036): at 
> android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.view.Choreographer.doCallbacks(Choreographer.java:562) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.view.Choreographer.doFrame(Choreographer.java:532) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> android.os.Handler.handleCallback(Handler.java:725) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.os.Handler.dispatchMessage(Handler.java:92) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> android.os.Looper.loop(Looper.java:137) 04-26 22:47:12.440: 
> E/AndroidRuntime(4036): at 
> android.app.ActivityThread.main(ActivityThread.java:5041) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> java.lang.reflect.Method.invokeNative(Native Method) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> java.lang.reflect.Method.invoke(Method.java:511) 04-26 22:47:12.440: 
> E/AndroidRuntime(4036): at 
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
> 04-26 22:47:12.440: E/AndroidRuntime(4036): at 
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-26 
> 22:47:12.440: E/AndroidRuntime(4036):  at 
> dalvik.system.NativeStart.main(Native Method) 
+2

該視圖尚未在'onCreate'中創建(因爲'onCreateView()'在Fragment生命週期中的'onCreate'後運行)。 – Luksprog 2013-04-26 12:50:45

+0

那麼遵循的正確過程是什麼?我正在考慮活動創建,但我不確定根據android設計原則這是否被認爲是猶太潔食... – tweetypi 2013-04-26 12:55:54

+0

從'onCreateView()'或'onActivityCreated()'訪問視圖。 – Luksprog 2013-04-26 12:56:59

回答

1

你不應該調用,而不是R.id.expensemainlayout

+0

我不知道,應該嗎?自從我開發android開發已經有一段時間了,只是想記住它是如何工作的。 – tweetypi 2013-04-26 12:58:32

1

視圖Luksprog說的是完全正確的。檢查片段生命週期的here。你可以試試下面的代碼:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.activity_expense_selection, container, false); 

    ViewGroup myViewGroup = (ViewGroup)v.findViewById(R.id.expensemainlayout); 
    ExpenseIconGridGenerate.GenerateIcons(getActivity(), myViewGroup); 

    return v; 
} 

你也可以刪除你的onCreate它是目前使用這種方式。

相關問題