为什么我的setVisibility()
方法输入NullPointException
?我不明白,我无法修复它,请问我该如何修复它,我需要在checkbox
中显示pridajActivity
设置。
我有SviatokPridajActivity
使用SviatokCursorFragment
和sviatok_pridaj.xml
,sviatok_simple_list_item.xml
错误位于check.setVisibility(View.VISIBLE);
PridajActivity.java
public class SviatokPridajActivity extends Activity
{
private DatabaseOp mDbHelper;
ListView listview;
String username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sviatok_pridaj);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
listview = (ListView) findViewById(R.id.listSviatok);
CheckBox check = (CheckBox)findViewById(R.id.checkBox);
check.setVisibility(View.VISIBLE);
showUserSettings();
mDbHelper = new DatabaseOp(this);
mDbHelper.open();
Cursor sviatokCursor = mDbHelper.fetchAllSviatokNastav(username, 3);
if (sviatokCursor.getCount()==0)
{
mDbHelper.naplnSviatky(username);
sviatokCursor = mDbHelper.fetchAllSviatokNastav(username, 3);
}
final SviatokCursorAdapter adapter = new SviatokCursorAdapter(this, sviatokCursor);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int stlpec, long arg3)
{
// TODO Auto-generated method stub
Cursor cur = (Cursor) adapter.getItem(stlpec);
String odosli = cur.getString(cur.getColumnIndex("_id"));
String zobraz = cur.getString(cur.getColumnIndex("dlzka"));
CheckBox check = (CheckBox)findViewById(R.id.checkBox);
if (Integer.parseInt(zobraz)==0)
{
mDbHelper.updateSviatok(odosli, username, 1);
} else {
mDbHelper.updateSviatok(odosli, username, 0);
}
check.setChecked(!check.isChecked());
//adapter.notifyDataSetChanged();
Cursor reloadedCursor= mDbHelper.fetchAllSviatokNastav(username, 3);
adapter.changeCursor(reloadedCursor);
}
});
}
@Override
public void onPause()
{
super.onPause();
mDbHelper.close();
}
private void showUserSettings()
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
username = sharedPrefs.getString("prefUsername", "NULL");
}
}
SviatokCursorAdapter.java
public class SviatokCursorAdapter extends CursorAdapter {
public SviatokCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView textViewUlohaDate = (TextView) view.findViewById(R.id.datumSviatku);
textViewUlohaDate.setText(cursor.getString(cursor.getColumnIndex("datum")));
TextView textViewUlohaName = (TextView) view.findViewById(R.id.nazovSviatku);
textViewUlohaName.setText(cursor.getString(cursor.getColumnIndex("nazov")));
CheckBox check = (CheckBox) view.findViewById(R.id.checkBox);
String checked = cursor.getString(cursor.getColumnIndex("dlzka"));
if (Integer.parseInt(checked)==1)
{
check.setChecked(true);
} else {
check.setChecked(false);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.sviatok_simple_list_item, parent,false);
return retView;
}
}
Sviatok_simple_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:visibility="gone"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="@+id/datumSviatku"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.5"
android:textSize="15sp"
android:textColor="#33b5e5"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp" />
<TextView
android:id="@+id/nazovSviatku"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:lines= "1"
android:textSize="20sp"
android:textColor="#656565"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
</LinearLayout>
sviatok_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/listSviatok"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Sviatok_pridaj.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff"
android:text="@string/pridat_sv" />
<ListView
android:id="@+id/listSviatok"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 0 :(得分:0)
您的Checkbox
中没有sviatok_pridaj.xml
,这就是findViewById
返回null
个对象的原因。
如果您尝试从Checkbox
访问onItemClick
,那么您应该使用此代码
CheckBox check = (CheckBox)arg1.findViewById(R.id.checkBox);
答案 1 :(得分:0)
您已设置 setContentView(R.layout.sviatok_pridaj),但您尝试访问 R.layout.Sviatok_simple_list_item 中的复选框。