我敢肯定,我做了一些愚蠢的錯誤......機器人:複選框空對象引用
所有我想要做的就是編程選中一個複選框。這是代碼:
CheckBox mycheck;
mycheck = (CheckBox) getView().findViewById(R.id.edu1415_3);
mycheck.setChecked(true);
我認爲問題是在一個片段中。我只是不知道如何解決它。我試圖在這兩個的onCreate方法,但我得到這個錯誤:試圖調用虛擬方法無效android.widget.CheckBox.setChecked(布爾)'對空對象引用
我缺少什麼?我感謝幫助!
這裏是我的文件:
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>
確保'edu1415.xml'文件已加載到內存中。順便說一下,我在'checklist1415_2.java'文件中找不到'findViewById()'。 –
如何將「edu1415.xml」加載到內存?我拿出'findViewById()',因爲它一直在打破。 –
問題中的findViewById不在您粘貼的代碼 – njzk2