簡體   English   中英

嘗試在Android上調用虛擬方法

[英]Attempt to invoke virtual method on Android

從可序列化類調用方法時出現錯誤。 我不能使用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()

不知道這個問題是什么...

您列表中的一項為null

嘗試添加

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

}

在for循環中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM