我正在使用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;
}
}
然后这个类用片段事务设置片段
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 :(得分:0)
要使用fragment
,Activity类需要从FragmentActivity
扩展,但正如我在SetDiningCommonsActivity.java
中看到的那样,它会扩展Activity。可能正在改变它将解决错误。
答案 1 :(得分:0)
您尚未为自己的活动设置任何视图。我们可以看到setContentView(R.layout.activity_layout);
中缺少MainActivity
。因为没有任何活动视图。
您的cardsview
可能会考虑片段的另一种可能性,所以
mCardView = (CardUI) getActivity().findViewById(R.id.cardsview);
未在onCreate()
DiningCommonsFragment
中初始化