android:Checkbox null对象引用

时间:2016-09-12 02:04:11

标签: android checkbox

我确定我做了一些愚蠢的错误......

我想要的是以编程方式检查复选框。这是代码:

CheckBox mycheck;
mycheck = (CheckBox) getView().findViewById(R.id.edu1415_3);
mycheck.setChecked(true);

我认为问题是在片段中进行。我只是不知道如何解决它。我已尝试过两种onCreate方法,但我收到此错误:尝试在空对象引用上调用虚方法'void android.widget.CheckBox.setChecked(boolean)'

我错过了什么?我很感激帮助!

以下是我的文件:

checklist1415_2.java:

package com.abc.paplanningforthefuturechecklist;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.CheckBox;
import android.widget.TextView;

public class checklist1415_2 extends AppCompatActivity {


    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checklist1415_2);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        //checkTheBoxes();



    }

    public void onChecklistChecked(View view){
        boolean checked = ((CheckBox) view).isChecked();
        String checkedItem = getResources().getResourceEntryName(view.getId());
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        int checkVal = 1;
        String pref_key = checkedItem;
        if(checked){
             editor.putInt(pref_key, checkVal);
             editor.commit();
        } else {
             editor.putInt(pref_key, -1);
             editor.commit();
        }
        System.out.println(pref_key);
    }    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.menu_checklist1415_2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

   public static class PlaceholderFragment extends Fragment {


        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View A = inflater.inflate(R.layout.edu1415, container, false);
            View B = inflater.inflate(R.layout.employ1415, container, false);
            View C = inflater.inflate(R.layout.comm1415, container, false);

            switch (getArguments().getInt(ARG_SECTION_NUMBER)){
                case 1:
                    return A;
                case 2:
                    return B;
                case 3:
                    return C;
            }

            return null;
        }
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
          return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
                case 2:
                    return "SECTION 3";
            }
            return null;
        }
    }
}

edu1415.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edu1415_1"
        android:text="@string/edu1415_1"
        android:paddingTop="20dp" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edu1415_2"
        android:text="@string/edu1415_2" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edu1415_3"
        android:text="@string/edu1415_3"
        android:onClick="onChecklistChecked"/>

</LinearLayout>

fragment_checklist1415_2.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.abc.paplanningforthefuturechecklist.checklist1415_2$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

如果要在片段中查找视图,请在onCreateView()中执行:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.edu1415, container, false);
    CheckBox checkbox = (CheckBox) root.findViewById(R.id.checkbox);
    return root;
}

由于getView()仅在onCreateView()之后有效,因此您无法在片段的onCreate()onCreateView()方法中使用它。