【发布时间】:2020-07-14 04:17:21
【问题描述】:
在我的应用程序中,单击按钮时会生成一个新片段。
activity_main.xml 文件中有LinearLayout。我想在创建片段时同时以语法方式将 Cardview 添加到 LinearLayout 中。所以我将卡片视图创建代码放在片段的oncreate() 方法中。
没有任何错误显示。但是当我点击主活动中的按钮时,应用程序崩溃了。
下面是片段代码。
package com.example.infinitybrowser;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import static com.example.infinitybrowser.R.id.recycle_container;
public class frag_one extends Fragment
{
Button new_tab;
TextView test_tab_no;
private Context context;
LinearLayout.LayoutParams layoutparams;
RelativeLayout relativeLayout;
LinearLayout linearLayout;
TextView textview;
private AttributeSet attrs;
View v;
ViewGroup container;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
// "recycle_container" is the linearlayout in activity_main.xml file
// Here "view.findViewById" showing error also??
linearLayout = (LinearLayout) v.findViewById (recycle_container);
//linearLayout = (LinearLayout)MainActivity.v;
context = context.getApplicationContext ();
context= container.getContext();
CardView tab_card = new CardView(context);
layoutparams = new LinearLayout.LayoutParams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tab_card.setLayoutParams(layoutparams);
tab_card.setRadius(15);
tab_card.setPadding(25, 25, 25, 25);
tab_card.setCardBackgroundColor(Color.MAGENTA);
tab_card.setMaxCardElevation(30);
tab_card.setMaxCardElevation(6);
textview = new TextView (context);
textview.setLayoutParams(layoutparams);
textview.setText("CardView Programmatically\n");
textview.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
textview.setTextColor(Color.WHITE);
textview.setPadding(25,25,25,25);
textview.setGravity(Gravity.CENTER);
tab_card.addView(textview);
linearLayout.addView(tab_card);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_frag_one,container,false);
return view;
}
}
但是当我点击按钮时,这是在logcat错误下面;
Process: com.example.infinitybrowser, PID: 30120
java.lang.NullPointerException
at com.example.infinitybrowser.frag_one.onCreate(frag_one.java:45)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1197)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1080)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:119)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:838)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:654)
at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
标签: java android android-studio android-fragments android-cardview