尝试在 Android 上调用虚拟方法
Posted
技术标签:
【中文标题】尝试在 Android 上调用虚拟方法【英文标题】:Attempt to invoke virtual method on Android 【发布时间】:2015-06-21 17:52:31 【问题描述】:从可序列化类调用方法时出现错误。 我不能在片段中使用另一个类的方法吗? 我有一个可序列化的类“Memo.java”
package com.example.user.memoapp;
import java.io.Serializable;
/**
* Serializable
* Created by user on 14/4/2015.
*/
public class Memo implements Serializable
private Boolean unfinished;
private String title,descri,markas;
private int day,month,year,hour,minute;
public Memo(Boolean unfinished, String title, String descri, String markas, int day, int month, int year, int hour, int minute)
this.unfinished = unfinished;
this.title = title;
this.descri = descri;
this.markas = markas;
this.day = day;
this.month = month;
this.year = year;
this.hour = hour;
this.minute = minute;
public boolean getUnfinished()
return unfinished;
public String getTitle()
return title;
public String getMarkas()
return markas;
public String getDescri()
return descri;
public int getYear()
return year;
public int getDay()
return day;
public int getMonth()
return month;
public int getMinute()
return minute;
public int getHour()
return hour;
我将它应用于片段类名称“All.java”
package com.example.user.memoapp;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Objects;
/**
* A simple @link Fragment subclass.
* Activities that contain this fragment must implement the
* @link All.OnFragmentInteractionListener interface
* to handle interaction events.
* Use the @link All#newInstance factory method to
* create an instance of this fragment.
*/
public class All extends Fragment
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String MEMO = "memo";
// TODO: Rename and change types of parameters
private Memo memo;
private ArrayList<Memo> mList = new ArrayList<Memo>();
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param memo Get Arraylist data from main activity
* @return A new instance of fragment all.
*/
// TODO: Rename and change types and number of parameters
public static All newInstance(Memo memo)
All fragment = new All();
Bundle args = new Bundle();
args.putSerializable(MEMO, memo);
fragment.setArguments(args);
return fragment;
public All()
// Required empty public constructor
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
if (getArguments() != null)
setMemo((Memo) getArguments().getSerializable(MEMO));
mList.add(getMemo());
public void addTableRow(Context context,View view)
TableLayout table = (TableLayout)view.getRootView().findViewById(R.id.all_tablelayout);
ArrayList<Memo> list = getmList();
for(int i = 0; i < list.size(); i++)
setMemo(list.get(i));
if(!getMemo().getUnfinished())
TableRow row = new TableRow(context);
TextView text = new TextView(context);
text.setText(memo.getTitle());
row.addView(text);
table.addView(row);
else
//show nothing
//If memo is unfinished will directly put into fragment_done
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_all, container, false);
if(getmList().size() != 0)
addTableRow(rootView.getContext(),rootView);
return rootView;
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri)
if (mListener != null)
mListener.onFragmentInteraction(uri);
public ArrayList<Memo> getmList()
return this.mList;
public void setMemo(Memo memo)
this.memo = memo;
public Memo getMemo()
return this.memo;
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
/*
try
mListener = (OnFragmentInteractionListener) activity;
catch (ClassCastException e)
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
*/
@Override
public void onDetach()
super.onDetach();
mListener = null;
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
Logcat报错:
04-15 16:10:58.538 2435-2435/com.example.user.memoapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.memoapp, PID: 2435
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.user.memoapp.Memo.getUnfinished()' on a null object reference
at com.example.user.memoapp.All.addTableRow(All.java:74)
at com.example.user.memoapp.All.onCreateView(All.java:94)
这是
if(!getMemo().getUnfinished())
基本上这不起作用
memo.getTitle()
不知道这是什么问题...
【问题讨论】:
【参考方案1】:您列表中的一项是null
尝试添加
if(list.get(i)!=null)
在 for 循环中。
【讨论】:
列表为null
,程序运行但行未添加到视图
我发现问题发生在从 MainActiviting 传递可序列化时,但完全是另一个问题,感谢您提供有用的测试方法'以上是关于尝试在 Android 上调用虚拟方法的主要内容,如果未能解决你的问题,请参考以下文章
尝试在空对象引用上调用虚拟方法 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()'
尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'
尝试在 getActivity() 上调用虚拟方法 'java.lang.String android.content.Context.getPackageName()
尝试在 nul 对象引用上调用虚拟方法 'android.content.ContentResolverr android.id.content.Context.getContentesolver()
尝试在空对象引用上调用虚拟方法 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()'