我绝望地尝试了互联网上的一切,以便能够打电话给我
thisIsAMethodJustToTryOutMyButton(View view)
,当用户点击我的Delete Button
MainActivity时,但无效。按钮没有反应。
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
适配器类
public class Adapter extends BaseAdapter{
private ArrayList<item> items= new ArrayList<>();
public void addItemToListView(Item item){
this.items.add(item);
}
public void deleteItem(int position){
items.remove(position);
}
@Override
public Item getItem(int position) {
return items.get(position);
}
@Override
public long getIdOfItem(int position) {
return position;
}
@Override
public int getCount() {
return items.size();
}
class ViewHolder {
public TextView title, content;
public ViewHolder(View view){
title= (TextView) view.findViewById(R.id.title);
content= (TextView) view.findViewById(R.id.content);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(null == convertView){
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.custom_row, parent, false);
viewHolder = new ViewHolder(convertView);
viewHolder.header.setTag(position);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
Item item= getItem(position);
viewHolder.header.setText(item.title);
viewHolder.bodyText.setText(item.content);
return convertView;
}
}
MyDialogForDeletingAItem类
public class MyDialogForDeletingAItem extends DialogFragment
implements View.OnClickListener {
Button delete;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.deleteDialog, null);
delete = (Button) view.findViewById(R.id.delete);
delete.setOnClickListener(this);
return view; }
@Override
public void onClick(View view) {
if (view.getId() == R.id.delete) {
MainActivity main = new MainActivity();
main.thisIsAMethodJustToTryOutMyButton(view, //This is where the problem is...
//I don't know what to pass in here);
}
MainActivity类
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {
Adapter adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.ListView);
adapter= new Adapter();
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(this);
listView.setOnItemClickListener(this);
public void longItemClick(View view) {
DialogFragment dialog = new MyDialogForDeletingAItem();
dialog.show(this.getSupportFragmentManager(), "MyDialogForDeletingAItem");
thisIsAMethodJustToTryOutMyButton(view);
}
public void deleteRow(int position){
adapter.delete(position);
adapter.notifyDataSetChanged();
}
public void thisIsAMethodJustToTryOutMyButton(View view, int position){
deleteRow(position);
Toast.makeText(MainActivity.this,"" + position, Toast.LENGTH_SHORT).show();
}
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DELETE"
android:id="@+id/delete"
android:clickable="true"
android:layout_alignTop="@+id/edit"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="102dp"
android:layout_marginEnd="102dp" />
at se.example.android.myapplication.MainActivity.thisIsAMethodJustToTryOutMyButton
(MainActivity.java:100)
at se.example.android.myapplication.DeleteAndEdit.onClick(DeleteAndEdit.java:45)
答案 0 :(得分:1)
在您需要使用已加载的活动实例时,onClick(View view)
方法会创建MainActivity
的新实例。
像这样打电话
@Override
public void onClick(View view) {
if (view.getId() == R.id.delete) {
((MainActivity) getActivity()).thisIsAMethodJustToTryOutMyButton(view);
}
答案 1 :(得分:0)
您的deleteRow()
方法应将View视为参数
public void deleteRow(View view)
public static final int onClick
在API级别4中添加此视图上下文中方法的名称 单击视图时调用。此名称必须与公众对应 只接受一个View类型参数的方法。例如,如果 你指定android:onClick =&#34; sayHello&#34;,你必须声明一个公共空白 sayHello(View v)您的上下文方法(通常是您的Activity)。
必须是字符串值,使用&#39; \;&#39;逃避诸如&#39; \ n&#39;等人物 或者&#39; \ uxxxx&#39;对于unicode角色。
这也可能是对资源的引用(在表单中) &#34; @ [package:] type:name&#34;)或theme属性(在表单中 &#34;?[package:] [type:] name&#34;)包含此类型的值。
答案 2 :(得分:0)
在您的MainActivity中,您在哪里引用thisIsAMethodJustToTryOutMyButton
?尝试在xml文件中添加android:onClick="thisIsAMethodJustToTryOutMyButton"