手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android Stidio 开发 初始化控件空指针
Android Stidio 开发 初始化控件空指针
摘要:代码如下: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);

【Android Stidio 开发 初始化控件空指针】相关文章:

Android UI开发专题(二) 之绘图基础

Android开发之OpenGL ES 基础

Android开发常用小功能

Android adb shell命令合集

Android Studio使用教程图文详解

Android开发之线性布局

Android开源组件小结

Android开发实现HttpClient工具类

Android应用开发UI控件ImageSwitcher的使用

Android开发之电源管理

精品推荐
分类导航