Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试在活动中添加FooterView()时出现NullpointerException_Java_Android - Fatal编程技术网

Java 尝试在活动中添加FooterView()时出现NullpointerException

Java 尝试在活动中添加FooterView()时出现NullpointerException,java,android,Java,Android,我一直在寻找类似的问题,但我找不到任何让我更聪明的东西,如果我的研究失败了,请原谅 问题:我试图在显示可扩展列表视图的活动中添加带有按钮的页脚。如果有人能指出我做错了什么,那就太棒了 这是我的活动代码: public class QuestionActivityMek extends ExpandableListActivity { //private static final String LOG_TAG = "QuestionActivity2"; private ExpandableL

我一直在寻找类似的问题,但我找不到任何让我更聪明的东西,如果我的研究失败了,请原谅

问题:我试图在显示可扩展列表视图的活动中添加带有按钮的页脚。如果有人能指出我做错了什么,那就太棒了

这是我的活动代码:

public class QuestionActivityMek extends ExpandableListActivity {


//private static final String LOG_TAG = "QuestionActivity2";
private ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
//HashMap<String, List<String>> listDataChild;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mek_list_layout);
    ArrayList<String> groupNames = new ArrayList<String>();
    groupNames.add("1. TÄNK IGENOM UPPDRAGET");
    groupNames.add("2. IDENTIFIERA FAROR I UPPDRAGET");


    ArrayList<ArrayList<Question>> questions = new ArrayList<ArrayList<Question>>();
    ArrayList<Question> question = new ArrayList<Question>();
    question.add (new Question("Har jag klart för mig vad uppdraget innebär?", false));
    question.add (new Question("Har jag klart för mig hur jag tar mig till arbetsplatsen säkert?", false));
    question.add (new Question("Har jag klart för mig vilka arbetsmoment som ingår i uppdraget?", false));
    questions.add(question);
    question = new ArrayList<Question>();
    question.add(new Question("Riskerar jag inte att snubbla/halka/falla? Check underlag,skyddsräcken, trappor, trappsteg och arbetsplatformar", true));
    question.add (new Question("Finns ingen påkörningsrisk, travers eller truck?", true));
    questions.add(question);
    listAdapter = new ExpandableListAdapter (this, groupNames, questions);

    //<<<<<Trying to add a button in the footer under the ExpandableListView>>>>>>
    final Button btnAddMore = new Button(this);
    btnAddMore.setText("continue");
    expListView = (ExpandableListView) this.findViewById(R.id.footer_layout);
    expListView.addFooterView(btnAddMore);
    //<<<<<<<<<<>>>>>>>>>>

    setListAdapter (listAdapter);


}
我的可扩展列表适配器:

public class QuestionActivityMek extends ExpandableListActivity {


//private static final String LOG_TAG = "QuestionActivity2";
private ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
//HashMap<String, List<String>> listDataChild;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mek_list_layout);
    ArrayList<String> groupNames = new ArrayList<String>();
    groupNames.add("1. TÄNK IGENOM UPPDRAGET");
    groupNames.add("2. IDENTIFIERA FAROR I UPPDRAGET");


    ArrayList<ArrayList<Question>> questions = new ArrayList<ArrayList<Question>>();
    ArrayList<Question> question = new ArrayList<Question>();
    question.add (new Question("Har jag klart för mig vad uppdraget innebär?", false));
    question.add (new Question("Har jag klart för mig hur jag tar mig till arbetsplatsen säkert?", false));
    question.add (new Question("Har jag klart för mig vilka arbetsmoment som ingår i uppdraget?", false));
    questions.add(question);
    question = new ArrayList<Question>();
    question.add(new Question("Riskerar jag inte att snubbla/halka/falla? Check underlag,skyddsräcken, trappor, trappsteg och arbetsplatformar", true));
    question.add (new Question("Finns ingen påkörningsrisk, travers eller truck?", true));
    questions.add(question);
    listAdapter = new ExpandableListAdapter (this, groupNames, questions);

    //<<<<<Trying to add a button in the footer under the ExpandableListView>>>>>>
    final Button btnAddMore = new Button(this);
    btnAddMore.setText("continue");
    expListView = (ExpandableListView) this.findViewById(R.id.footer_layout);
    expListView.addFooterView(btnAddMore);
    //<<<<<<<<<<>>>>>>>>>>

    setListAdapter (listAdapter);


}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人问题活动背景;
私有列表listDataHeader;
私有ArrayList listDataChild;
私人充气机;
私有布尔指示符;
//int-previousGroup=-1;
公共可扩展ListAdapter(QuestionActivityMek上下文,列表listDataHeader,
ArrayList(listChildData){
this.context=上下文;
this.listDataHeader=listDataHeader;
this.listDataChild=listChildData;
充气器=充气器。从(上下文);
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
返回listDataChild.get(groupPosition).get(childPosition);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
视图=空;
if(convertView!=null){
视图=转换视图;
}
//否则
视图=充气机。充气(R.layout.list_项,父项,false);
问题=(问题)getChild(groupPosition,childPosition);
TextView txtviewquestion=(TextView)view.findViewById(R.id.question);
//ExpandableListView explistview=(ExpandableListView)view.findViewById(R.layout.mek\u list\u layout);
按钮btnPos=(按钮)视图.findViewById(R.id.btnPositive);
按钮btnNeg=(按钮)视图.findViewById(R.id.btnNegative);
if(txtviewquestion!=null)
txtviewquestion.setText(question.getQuestion());
indicator=question.getIndicator();
btnPos.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//如果(指标){
//showRiskEvaluationDialog();
//}否则
Toast.makeText(上下文“保存”,Toast.LENGTH_SHORT).show();
}
});
btnNeg.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//如果(指标){
showRiskEvaluationDialog();
//Toast.makeText(上下文“保存”,Toast.LENGTH_SHORT).show();
//}否则
}
});
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回listDataChild.get(groupPosition.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此.listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回此.listDataHeader.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
视图=空;
//字符串头文件=(字符串)getGroup(groupPosition);
if(convertView!=null){
视图=转换视图;
}否则
视图=充气机。充气(R.layout.list\u组,父级,false);
字符串getgroup=(字符串)getgroup(groupPosition);
TextView问题组=(TextView)view.findViewById(R.id.lblistheader);
ExpandableListView explistview=(ExpandableListView)父级;
explistview.expandGroup(groupPosition);
if(getgroup!=null)
questiongroup.setText(getgroup);
返回视图;
}

这是具有ExpandableListView的xml:

public class QuestionActivityMek extends ExpandableListActivity {


//private static final String LOG_TAG = "QuestionActivity2";
private ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
//HashMap<String, List<String>> listDataChild;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mek_list_layout);
    ArrayList<String> groupNames = new ArrayList<String>();
    groupNames.add("1. TÄNK IGENOM UPPDRAGET");
    groupNames.add("2. IDENTIFIERA FAROR I UPPDRAGET");


    ArrayList<ArrayList<Question>> questions = new ArrayList<ArrayList<Question>>();
    ArrayList<Question> question = new ArrayList<Question>();
    question.add (new Question("Har jag klart för mig vad uppdraget innebär?", false));
    question.add (new Question("Har jag klart för mig hur jag tar mig till arbetsplatsen säkert?", false));
    question.add (new Question("Har jag klart för mig vilka arbetsmoment som ingår i uppdraget?", false));
    questions.add(question);
    question = new ArrayList<Question>();
    question.add(new Question("Riskerar jag inte att snubbla/halka/falla? Check underlag,skyddsräcken, trappor, trappsteg och arbetsplatformar", true));
    question.add (new Question("Finns ingen påkörningsrisk, travers eller truck?", true));
    questions.add(question);
    listAdapter = new ExpandableListAdapter (this, groupNames, questions);

    //<<<<<Trying to add a button in the footer under the ExpandableListView>>>>>>
    final Button btnAddMore = new Button(this);
    btnAddMore.setText("continue");
    expListView = (ExpandableListView) this.findViewById(R.id.footer_layout);
    expListView.addFooterView(btnAddMore);
    //<<<<<<<<<<>>>>>>>>>>

    setListAdapter (listAdapter);


}


猜测
R.id.footer\u布局是错误的,那一定是您的页脚名称,或者是因为找不到页脚而出错了

我想您的问题是这一行

expListView = (ExpandableListView) this.findViewById(R.id.footer_layout);

这一行实际上并没有返回视图,而是返回null。
R.id.footer\u布局
实际上是可解释的列表视图吗?

可能是两个原因之一

  • 布局的类型转换错误

    expListView=(ExpandableListView)this.findViewById(R.id.footer\u布局)

  • 第二个expListView为空。请尝试将其更改为:

    if(expListView!=null){ expListView.addFooterView(btnAddMore); }


  • 好的,通过实现以下代码解决了这个问题

        //<<<<<Trying to add a button in the footer under the ExpandableListView>>>>>>
        final Button btnAddMore = new Button(this);
        btnAddMore.setText("continue");
        ExpandableListView explistview = getExpandableListView(); //<-- Used this methodcall instead
        explistview.addFooterView(btnAddMore);
    
        //<<<<<<<<<<>>>>>>>>>>
        setListAdapter (listAdapter);
    
    />
    最终按钮btnAddMore=新按钮(此按钮);
    btnAddMore.setText(“继续”);
    
    ExpandableListView explistview=getExpandableListView();//问题活动测试的第69行是什么?它是explistview=(ExpandableListView)this.findViewById(R.id.footer\u布局);行,那么,您的ExpandableListView被命名为footer_layout?啊,不,哈哈,我犯了个愚蠢的错误。我为ExpandableListView添加了布局,但它仍然崩溃,并给了我相同的错误:/this.findViewById(R.id.footer_layout)不可能使用
    ExpressView=(ExpandableListView)this.findViewById(R.id.footer_layout);
    直接生成
    NullPointerException
    。要么堆栈跟踪错误,要么源代码错误,要么不是第69行。-1的作用是什么?这个帮助是错误的还是放错了位置?
        //<<<<<Trying to add a button in the footer under the ExpandableListView>>>>>>
        final Button btnAddMore = new Button(this);
        btnAddMore.setText("continue");
        ExpandableListView explistview = getExpandableListView(); //<-- Used this methodcall instead
        explistview.addFooterView(btnAddMore);
    
        //<<<<<<<<<<>>>>>>>>>>
        setListAdapter (listAdapter);