Je reçois une erreur lors de l'appel d'une méthode de la classe sérialisable. Impossible d'utiliser une méthode d'une autre classe dans un fragment? J'ai une classe sérialisable "Memo.java"Tentative d'invocation de la méthode virtuelle sur Android
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;
}
}
Et je demande sur un nom de classe 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 signaler une erreur:
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)
qui est cette
if(!getMemo().getUnfinished()){
essentiellement ce ne fonctionnent pas à
memo.getTitle()
aucune idée de ce que ce problème est ...
la liste est 'null', le programme a fonctionné mais la ligne n'a pas été ajoutée à la vue –
J'ai découvert que le problème se produisait lors du passage sérialisable de MainActiviting, mais c'est une toute autre question, merci pour votre méthode de test utile ' –