Android Stidio 开发 初始化控件空指针_安卓软件开发教程-查字典教程网
Android Stidio 开发 初始化控件空指针
Android Stidio 开发 初始化控件空指针
发布时间:2018-02-01 来源:查字典编辑
摘要:代码如下:publicclassTimeDialogextendsDialog{privateImageButtonquit;private...

代码如下:

public class TimeDialog extends Dialog {

    private ImageButton quit;

    private TextView calendar;

    private ImageButton close;

    private FixedGridView times;

    private Button btn;

    public TimeDialog(Context context) {

        super(context, R.style.ShadowDialog);

    }

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_time);

        Window window = getWindow();

        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        window.setWindowAnimations(R.style.dialogAnimBottom); 

        setCanceledOnTouchOutside(false);

        quit = (ImageButton) findViewById(R.id.time_quit);

        calendar = (TextView) findViewById(R.id.time_calendar);

        close = (ImageButton) findViewById(R.id.time_close);

        times = (FixedGridView) findViewById(R.id.time_times);

        btn = (Button) findViewById(R.id.time_btn);

        data = new ArrayList< >();

        adapter = new TimeAdapter(data);

        times.setAdapter(adapter);

    }

}

布局:

< ?xml version="1.0" encoding="utf-8"? >

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center"

    android:padding="@dimen/common_20dp" >

    < LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:padding="@dimen/common_20dp"

        android:gravity="center"

        android:orientation="horizontal"

        android:background="@color/white" >

        < ImageButton

            android:id="@+id/time_quit"

            android:layout_width="@dimen/common_25dp"

            android:layout_height="@dimen/common_40dp"

            android:background="@mipmap/btn_quit_gray"/ >

        < LinearLayout

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:layout_marginLeft="@dimen/common_15dp"

            android:gravity="center"

            android:orientation="vertical" >

            < TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="请选择服务日期"

                android:textSize="@dimen/common_28sp"

                android:textColor="@color/black"/ >

            < TextView

                android:id="@+id/time_calendar"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="@dimen/common_10dp"

                android:text="请选择服务日期"

                android:textSize="@dimen/common_26sp"

                android:textColor="@color/txt_gray_80"/ >

        < /LinearLayout >

        < ImageButton

            android:id="@+id/time_close"

            android:layout_width="@dimen/common_40dp"

            android:layout_height="@dimen/common_40dp"

            android:background="@mipmap/btn_close_gray"/ >

    < /LinearLayout >

    < LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:padding="@dimen/common_20dp"

        android:background="@color/offwhite"

        android:gravity="center"

        android:orientation="vertical" >

        < com.paisen.d.beautifuknock.view.FixedGridView

            android:id="@+id/time_times"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:verticalSpacing="@dimen/common_10dp"

            android:horizontalSpacing="@dimen/common_10dp"

            android:gravity="center"

            android:numColumns="4"/ >

    < /LinearLayout >

    < LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:padding="@dimen/common_20dp"

        android:gravity="center"

        android:background="@color/white"

        android:orientation="horizontal" >

        < Button

            android:id="@+id/time_btn"

            android:layout_width="@dimen/common_180dp"

            android:layout_height="@dimen/common_80dp"

            android:background="@drawable/bg_yellow"

            android:text="确定"

            android:textColor="@color/white"

            android:textSize="@dimen/common_28sp"/ >

    < /LinearLayout >

< /LinearLayout >

回复讨论(解决方案)

因为存在LinearLayout的嵌套,获取LinearLayout内控件的值需要先获取LinearLayout后再去找里面的控件。

比如:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

ImageButton b1 = (ImageButton)layout.findViewById(R.id.b1);

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新安卓软件开发学习
    热门安卓软件开发学习
    编程开发子分类