尝试在Android上调用虚拟方法

时间:2015-04-15 16:32:21

标签: java android android-fragments serializable

从可序列化类调用方法时出现错误。 我不能在Fragment中使用另一个类的方法吗? 我有一个可序列化的类“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;
}
}

我将它应用于Fragment类名“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 个答案:

答案 0 :(得分:0)

列表中的一个项目是null

尝试添加

if(list.get(i)!=null){

}
在for循环中