Java 尝试在运行时在expandablelistview android studio中创建按钮时出现NPE错误
我试图在运行时在expandablelistview中创建按钮,当您单击listview的一个元素时Java 尝试在运行时在expandablelistview android studio中创建按钮时出现NPE错误,java,android,button,expandablelistview,Java,Android,Button,Expandablelistview,我试图在运行时在expandablelistview中创建按钮,当您单击listview的一个元素时 public class ExpandAnimationDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //To change body of over
public class ExpandAnimationDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //To change body of overridden methods use File | Settings | File Templates.
setContentView(R.layout.main);
ListView list = (ListView)findViewById(R.id.udiniList);
final LinearLayout lm = (LinearLayout) findViewById(R.id.toolbar);
ArrayAdapter<String> listAdapter = new CustomListAdapter(this, R.layout.list_item);
for (int i=0; i<5;i++)
listAdapter.add("udini"+i);
// create the layout params that will be used to define how your
// button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
// Create Button
final Button btn = new Button(this);
// Give button an ID
//btn.setId(j+1);
btn.setText("Add To Cart");
// set the layoutParams on the button
btn.setLayoutParams(params);
//Add button to LinearLayout
ll.addView(btn);
//Add button to LinearLayout defined in XML
lm.addView(ll); here i have de null pointer!
list.setAdapter(listAdapter);
// Creating an item click listener, to open/close our toolbar for each item
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
View toolbar = view.findViewById(R.id.toolbar);
// Creating the expand animation for the item
ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500);
// Start the animation on the toolbar
toolbar.startAnimation(expandAni);
}
});
}
/**
* A simple implementation of list adapter.
*/
class CustomListAdapter extends ArrayAdapter<String> {
public CustomListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, null);
}
((TextView)convertView.findViewById(R.id.title)).setText(getItem(position));
// Resets the toolbar to be closed
View toolbar = convertView.findViewById(R.id.toolbar);
((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50;
toolbar.setVisibility(View.GONE);
return convertView;
}
}
}
但我得到一个空指针异常
原因:java.lang.NullPointerException
在com.udinic.expand\u animation\u example.ExpandAnimationDemo.onCreateExpandAnimationDemo.java:51
-->这里是lm.addviewl
R.id.toolbar存在于main.xml中首先,感谢您花时间回答我,您是对的,但没有向我显示任何内容,因为很明显,我想显示的是list_item.xml,您可以在这里找到R.id.toolbar,我该怎么办?我是新手,如果所有与lm变量相关的变量都属于列表项,对吗?那么该代码应该在适配器中,而不是在活动中。该活动已加载main.xml,其中R.id.toolbar不存在,您正在尝试使用它。所以,它抛出了NPE。检查将sysout置于lm变量之下,可以看到对象为null。