我有两个可行的应用程序。现在我已经制作了一个新的应用程序,它基本上是一个导航抽屉,我试图将这些其他应用程序分成一个片段。当我尝试调整其中一个应用程序的主要活动代码时,该片段崩溃了。没有警告,没有错误,只是崩溃..
我在这里留下一些片段代码(我正在切割一些线条,我确定这不是问题):
public class SFragment extends Fragment {
public SFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_s, container, false);
rdGroup1 = (RadioGroup)getView().findViewById(R.id.radioGroup1);
rdGroup2 = (RadioGroup)getView().findViewById(R.id.radioGroup2);
cBox1 = (CheckBox)getView().findViewById(R.id.especCB);
button = (Button)getView().findViewById(R.id.button1);
list = (ListView)getView().findViewById(R.id.listView1);
return rootView;
}
public String itemValue;
public String[] values;
private int espec = 0;
private int curso = 0;
private int cuatri = 0;
private ListView list;
private RadioGroup rdGroup1;
private RadioGroup rdGroup2;
private CheckBox cBox1;
private Button button;
ArrayAdapter<String> adapter;
DatabaseHandler db = new DatabaseHandler(getActivity());
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] {};
adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, values);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item value
itemValue = (String) list.getItemAtPosition(position);
// Show Alert
Toast.makeText(getActivity(), itemValue, Toast.LENGTH_LONG).show();
Intent i = new Intent(getActivity().getApplicationContext(), VistaAsig.class);
i.putExtra("itemValue", itemValue);
Log.v("MActivity onCreate","onItemClicklist Intent OK");
startActivity(i);
Log.v("MActivity onCreate","onItemClicklist Start OK");
}
});
rdGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
cBox1.setEnabled(true);
if (checkedId == R.id.radio1){
/*No hay especialidades en primero*/
curso = 1; cBox1.setEnabled(false);
}else if (checkedId == R.id.radio2){
/*No hay especialidades en segundo*/
curso = 2; cBox1.setEnabled(false);
}else if (checkedId == R.id.radio3){
curso = 3;
}else if (checkedId == R.id.radio4){
curso = 4;
}
if ((curso != 0)&&(cuatri != 0)) { button.setEnabled(true); }
}
});
rdGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == R.id.radio11){
cuatri = 1;
}else if (checkedId == R.id.radio12){
cuatri = 2;
}
if ((curso != 0)&&(cuatri != 0)) { button.setEnabled(true); }
}
});
cBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
if (espec == 0) espec = 1; else if (espec == 1) espec = 0;
}
});
addData();
}
答案 0 :(得分:1)
在这一行
rdGroup1 = (RadioGroup)getView().findViewById(R.id.radioGroup1);
rdGroup2 = (RadioGroup)getView().findViewById(R.id.radioGroup2);
cBox1 = (CheckBox)getView().findViewById(R.id.especCB);
button = (Button)getView().findViewById(R.id.button1);
list = (ListView)getView().findViewById(R.id.listView1);
将getView()
更改为rootView
getView()
检索您在onCreateView
中返回的视图,但您仍在onCreateView
中,因此它返回null,从而导致NullPointerException导致应用崩溃
答案 1 :(得分:0)
清理构建然后试试这个:
更改:
adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, values);
进入:......(ROOT,SOURCE,DESITNATION)
示例:
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.title, android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
如果您在Logcat中没有收到任何错误,那么您应该使用断点来查看它何时中断。
答案 2 :(得分:0)
感谢Jorge Alfaro,我找到了以下答案:
您应该像我一样在每个方法之外创建对象,并在onCreateView
内初始化对象。因此,在第一部分中,您将拥有:
DatabaseHandler db;
在onCreateView
内:
db = new DatabaseHandler(getActivity());