-1

我正在使用 CardUi Lib 并尝试在主要活动中制作一张卡片,打开一个带有更多卡片的片段。(基本上是一个列表)

所以我创建了一个包含卡片的片段。

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;
}
}

然后这个类用fragment transacation设置fragment

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);

任何帮助将不胜感激。

编辑:

日志猫

 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>
4

2 回答 2

0

对于使用fragment,Activity 类需要扩展,FragmentActivity但正如我在您的 中看到的SetDiningCommonsActivity.java,它扩展了 Activity。可能正在更改它将解决错误。

于 2013-08-29T13:06:14.360 回答
0

您尚未为您的活动设置任何视图。正如我们所见,您setContentView(R.layout.activity_layout);MainActivity. 因为没有任何活动视图。

那么您可能会看到片段的另一种可能性cardsview,所以

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

onCreate()未初始化DiningCommonsFragment

于 2013-08-29T13:14:59.050 回答