2013-08-29 108 views
-1

我正在使用CardUi Lib並試圖在主要活動中製作卡片以打開更多卡片。 (基本上是一個列表)NullPoinerException當開始在活動中開始片段時

所以我創建了一個包含卡的片段。

DiningCommonsFragment.java

public class DiningCommonsFragment extends Fragment { 


private CardUI mCardView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    // init CardView 
    mCardView = (CardUI) getActivity().findViewById(R.id.cardsview); 

    mCardView.setSwipeable(false); 

    CardStack diningCommonsStack = new CardStack(); 
    mCardView.addStack(diningCommonsStack); 

    MyPlayCard auxServiceWarehouseCard = new MyPlayCard("Auxilary Services Warehouse ", 
      null, "#99CC00", 
      "#669900", true, true); 

      auxServiceWarehouseCard.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=Auxilary+Services+Warehouse+UMass+Amherst+Amherst+MA+01003&mode=w")); 
      startActivity(intent); 
      } 
      }); 

    mCardView.addCard(auxServiceWarehouseCard); 

} 

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 

    // Inflate the layout for this fragment 
    View V = inflater.inflate(R.layout.diningcommons_fragment, container, false); 

    return V; 
} 
} 

那麼這個類來設定片段transacation片段

SetDiningCommonsActivity.java

public class SetDiningCommonsActivity extends Activity { 

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

    getFragmentManager().beginTransaction().replace(android.R.id.content, 
       new DiningCommonsFragment()).commit(); 
} 

} 

然後在我的主要活動是這樣的代碼鏈接到新的片段

MainActivity.java

@Override 
     public void onClick(View v) { 
      Intent intent = new Intent(); 
      intent.setClass(MainActivity.this, SetDiningCommonsActivity.class); 
      startActivityForResult(intent, 0); 
     } 

所以每當我點擊一個鏈接到新的片段,應用程序崩潰的卡。 Logcat中的NullPointer指向這一行。

 mCardView.setSwipeable(false); 

我相信它與這條線有關。

 mCardView = (CardUI) getActivity().findViewById(R.id.cardsview); 

任何幫助將不勝感激。

編輯:

logcat的

08-29 12:47:25.789: E/AndroidRuntime(1785): FATAL EXCEPTION: main 
08-29 12:47:25.789: E/AndroidRuntime(1785): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kc.umassguide/com.kc.umassguide.SetDiningCommonsActivity}: java.lang.NullPointerException 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.os.Looper.loop(Looper.java:137) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at java.lang.reflect.Method.invoke(Method.java:511) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at dalvik.system.NativeStart.main(Native Method) 
08-29 12:47:25.789: E/AndroidRuntime(1785): Caused by: java.lang.NullPointerException 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at com.kc.umassguide.DiningCommonsFragment.onCreate(DiningCommonsFragment.java:29) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.Fragment.performCreate(Fragment.java:1673) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:854) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.BackStackRecord.run(BackStackRecord.java:682) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.Activity.performStart(Activity.java:5113) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153) 
08-29 12:47:25.789: E/AndroidRuntime(1785):  ... 11 more 

diningcommons_fragment.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" 
    > 


    <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/umass_header" 
      android:contentDescription="@string/logo" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="10dp" 

      /> 

    <com.fima.cardsui.views.CardUI 
     android:id="@+id/cardsview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/imageView1" 

    /> 

</RelativeLayout> 
+0

粘貼例外 – allprog

+0

儘量延長片段活動,而不僅僅是活動。 – Rarw

+0

顯示您的'mCardView'所在的xml。它在'diningcommons_fragment.xml'中嗎?試試這個'View V = inflater.inflate(R.layout.diningcommons_fragment,container,false); mCardView =(CardUI)V.findViewById(R.id.cardsview)' – Raghunandan

回答

0

對於使用fragment,Activity類需要從FragmentActivity延伸,但正如我在你的SetDiningCommonsActivity.java看到,它擴展了Activity。可能會改變它會解決錯誤。

0

您尚未設置任何活動視圖。正如我們所看到的,您在MainActivity中缺少setContentView(R.layout.activity_layout);。因爲沒有任何積極的看法。

還有一個可能性,你的cardsview可能是鑑於片段的話,那麼

mCardView = (CardUI) getActivity().findViewById(R.id.cardsview); 

onCreate()初始化DiningCommonsFragment