添加新代码后片段崩溃

时间:2014-03-27 17:39:29

标签: android android-fragments listener

当我点击打开它时,我的活动崩溃了。首先它工作正常但后来我添加了一些新代码,现在它显示无法启动应用程序。新代码是一个复选框和onCheckedChange Listener,我无法找到出错的地方。

CrimeFragment

package com.bignerdranch.android.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CrimeFragment extends Fragment {
Crime mCrime;
EditText mTitleField;
Button b1;
CheckBox CB_solved;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCrime = new Crime();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) 
{
    View v = inflater.inflate(R.layout.fragment_crime, parent, false);

    mTitleField = (EditText) v.findViewById(R.id.crime_title);
    mTitleField.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence c, int start, int before, int count) {
            mCrime.setTitle(c.toString());

        }

        public void beforeTextChanged(CharSequence c, int start, int count, int after) {
            // this space intentionally left blank
        }

        public void afterTextChanged(Editable c) {
            // this one too
        }
    });    

    b1 = (Button) v.findViewById(R.id.button1);
    b1.setText(mCrime.getDate().toString());

    CB_solved = (CheckBox) v.findViewById(R.id.checkBox1);
    CB_solved.setOnCheckedChangeListener(new OnCheckedChangeListener(){
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            // TODO Auto-generated method stub
            mCrime.setCheck(arg1);
        }
    });
    return v; 
}
}

CrimeActivity

public class CrimeActivity extends FragmentActivity {
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crime);
    FragmentManager manager = getSupportFragmentManager();
    Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);

    if (fragment == null) {
        fragment = new CrimeFragment();
        manager.beginTransaction()
            .add(R.id.fragmentContainer, fragment)
            .commit();
    } 
}
}

0 个答案:

没有答案