【问题标题】:Android, setVisible and NullPointExceptionAndroid,setVisible 和 NullPointErexception
【发布时间】:2014-05-31 19:08:57
【问题描述】:

为什么我的setVisibility() 方法会输入NullPointException ??我不明白,我无法修复它,请问我该如何修复它,我需要在 pridajActivity 中设置可见的 checkbox

我有 SviatokPridajActivity 什么用 SviatokCursorFragmentsviatok_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>

【问题讨论】:

  • 为什么不在适配器的getView 方法中设置其可见性?
  • 因为我将它用于 2 个活动,所以在第一个复选框必须是第二个不能...它认为这是一个解决方案
  • 你能分享你的sviatok_pridaj.xml代码吗?

标签: android checkbox fragment visibility


【解决方案1】:

您的sviatok_pridaj.xml 中没有任何Checkbox,这就是findViewById 返回null 对象的原因。

如果您尝试从onItemClick 访问Checkbox,那么您应该使用此代码

CheckBox check = (CheckBox)arg1.findViewById(R.id.checkBox);

【讨论】:

    【解决方案2】:

    您已设置 setContentView(R.layout.sviatok_pridaj),但您正尝试从 R.layout.Sviatok_simple_list_item 访问复选框。

    【讨论】:

    • 那么?抱歉,我迷路了:(
    猜你喜欢
    • 2014-04-15
    • 2012-09-06
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多