在我的基础上输出类片段时出错

时间:2018-03-03 18:19:13

标签: android android-activity fragment android-inflate

我已经2小时才能找到一个我无法找到的解决方案。 我有2个活动,每个活动包含一个静态片段,一个活动显示任务列表,另一个活动详细说明所选任务。当我点击任务列表中的某个项目时,我会看到后面的日志,并且应用程序崩溃了。有人可以解释一下为什么它不起作用吗?

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: griffern.android.tp3, PID: 3575
              java.lang.RuntimeException: Unable to start activity ComponentInfo{griffern.android.tp3/griffern.android.tp3.DetailsActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2435)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
                  at android.app.ActivityThread.access$900(ActivityThread.java:154)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:153)
                  at android.app.ActivityThread.main(ActivityThread.java:5472)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
               Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
                  at android.app.Activity.setContentView(Activity.java:2215)
                  at griffern.android.tp3.DetailsActivity.onCreate(DetailsActivity.java:21)
                  at android.app.Activity.performCreate(Activity.java:6323)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2388)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:153) 
                  at android.app.ActivityThread.main(ActivityThread.java:5472) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
               Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
                  at android.app.Activity.setContentView(Activity.java:2215) 
                  at griffern.android.tp3.DetailsActivity.onCreate(DetailsActivity.java:21) 
                  at android.app.Activity.performCreate(Activity.java:6323) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2388) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:153) 
                  at android.app.ActivityThread.main(ActivityThread.java:5472) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
               Caused by: java.lang.NullPointerException: PUTAIN DE MERDE
                  at griffern.android.tp3.DetailsFragment.onViewCreated(DetailsFragment.java:59)
                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
                  at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1228)
                  at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2201)
                  at android.app.FragmentController.onCreateView(FragmentController.java:98)
                  at android.app.Activity.onCreateView(Activity.java:5631)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
                  at android.app.Activity.setContentView(Activity.java:2215) 
                  at griffern.android.tp3.DetailsActivity.onCreate(DetailsActivity.java:21) 
                  at android.app.Activity.performCreate(Activity.java:6323) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2388) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:153) 
                  at android.app.ActivityThread.main(ActivityThread.java:5472) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

现在,这是我的代码。

Class LesTachesActivity:

package griffern.android.tp3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class LesTachesActivity extends AppCompatActivity implements LesTachesInterface{

public static final String NOM_T = "name";
public static final String DUREE_T = "duration";
public static final String DESC_T = "description";
public static final String CAT_T = "categorie";
public static final String TACHE = "tache";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public void tacheSelectionnee(Tache t) {
    System.out.println("la tache " + t.getNom() + " est sélectionnée !");

    Intent intent = new Intent(this, DetailsActivity.class );

    intent.putExtra(NOM_T, t.getNom());
    intent.putExtra(DUREE_T, t.getDuree());
    intent.putExtra(DESC_T, t.getDescription());
    intent.putExtra(CAT_T, t.getCategorie());

    startActivity(intent);
}
}

Class LesTachesFragment:

package griffern.android.tp3;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Pierre on 16/02/2018.
 */

public class LesTachesFragment extends Fragment {

private List<Tache> mesDonnees;
private TacheAdapter adapter;
private ListView lvTache;
private TextView title_tache_liste;

private LesTachesInterface lesTachesInterface;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.taches_fragment, container, false);
    return v;
}

@Override
public void onViewCreated(View v, Bundle b) {
    super.onActivityCreated(b);
    mesDonnees = new ArrayList<Tache>();
    this.initTaches();
    adapter = new TacheAdapter(this.getActivity(), this.mesDonnees);
    title_tache_liste = (TextView) v.findViewById(R.id.title);
    title_tache_liste.setText(R.string.app_name);
    lvTache = (ListView) v.findViewById(R.id.list);
    lvTache.setAdapter(adapter);
    lvTache.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView taskTittleView = view.findViewById(R.id.taskText);
            String taskTittle = taskTittleView.getText().toString();
            System.out.println(taskTittle);

            for(Tache t : mesDonnees){
                if (t.getNom().equals(taskTittle)){
                    lesTachesInterface.tacheSelectionnee(t);
                    break;
                }
            }
        }
    });
}

private void initTaches() {
    mesDonnees.add(new Tache("Faire les courses", Categorie.COURSES, 40, "pain, fromage, fruit et légumes.."));
    mesDonnees.add(new Tache("Jouer avec les petits", Categorie.ENFANTS, 30, "Kappla, Lego, Cartes..."));
    mesDonnees.add(new Tache("Ranger ma chambre", Categorie.MENAGE, 60, ""));
    mesDonnees.add(new Tache("Faire du sport", Categorie.SPORT, 120, "Badminton ce jeudi"));
}

public void ajoutTache(Tache t){
    mesDonnees.add(t);
}

@TargetApi(23)
@Override
public void onAttach(Context context) {
    //This method avoid to call super.onAttach(context) if I'm not using api 23 or more
    if (Build.VERSION.SDK_INT >= 23) {
        super.onAttach(context);
        onAttachToContext(context);
    }
}

/*
* Deprecated on API 23
* Use onAttachToContext instead
*/
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (Build.VERSION.SDK_INT < 23) {
        onAttachToContext(activity);
    }
}

/*
* This method will be called from one of the two previous method
*/
protected void onAttachToContext(Context context) {
    try{
        lesTachesInterface = (LesTachesInterface) context ;
        System.out.println("test de OnAttach().");
    } catch (ClassCastException e){
        throw new ClassCastException((context.toString() + " must implement LesTachesInterface"));
    }
}
}

班级详情活动:

package griffern.android.tp3;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;

/**
 * Created by Pierre on 12/02/2018.
 */

public class DetailsActivity extends Activity {


private static final String TAG_DETAIL_FRAGMENT = "DetailFragment";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_tache_activity);

    Intent intent = this.getIntent();
    String taskTittle = intent.getStringExtra(LesTachesActivity.NOM_T);
    String taskDescription = intent.getStringExtra(LesTachesActivity.DESC_T);
    int taskDuration = intent.getIntExtra(LesTachesActivity.DUREE_T, -1);
    Categorie taskCategory = (Categorie) intent.getSerializableExtra((LesTachesActivity.CAT_T));

    System.out.println( taskTittle + "\n"
                        + taskDescription + "\n"
                        + taskDuration + "\n"
                        + taskCategory);



    Tache t = new Tache(taskTittle, taskCategory, taskDuration, taskDescription);

    DetailsFragment df = DetailsFragment.newInstance(t); //TODO

    System.out.println("SHOW ME THIS PLSSSSSSSSSSSSSSSSSSSS \n \n \n" + ((Tache) df.getArguments().getSerializable(LesTachesActivity.TACHE)).getNom() + " is a TROOOOLLLL \n \n \n");

    FragmentManager fm = getFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    transaction .add(R.id.detail_fragment, df, TAG_DETAIL_FRAGMENT)
                .addToBackStack(TAG_DETAIL_FRAGMENT)
                .commit();


}
}

班级详情片段:

package griffern.android.tp3;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Pierre on 16/02/2018.
 */

public class DetailsFragment extends Fragment {

protected Tache tache;

protected TextView tittleView;
protected TextView descView;
protected TextView durationView;
protected ImageView imgView;

public static DetailsFragment newInstance(Tache t){
    Bundle args = new Bundle();

    args.putSerializable(LesTachesActivity.TACHE, t);
    args.putString(LesTachesActivity.NOM_T, t.getNom());
    args.putString(LesTachesActivity.DESC_T, t.getDescription());
    args.putInt(LesTachesActivity.DUREE_T, t.getDuree());
    args.putSerializable(LesTachesActivity.CAT_T, t.getCategorie());
    args.putInt("test", 7);

    DetailsFragment df = new DetailsFragment();

    df.setArguments(args);

    System.out.println("WTF \n WTF \n WTF \n WTF \n Est ce que df possède bien les arguments ? : " + df.getArguments().getInt("test"));
    return df;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.detail_tache, container, false);
    return v;
}

@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);
    tittleView = (TextView) v.findViewById(R.id.taskTittle);
    descView = (TextView) v.findViewById(R.id.taskDescription);
    durationView = (TextView) v.findViewById(R.id.taskDuration);
    imgView = (ImageView) v.findViewById(R.id.taskImg);

    Bundle args = getArguments();
    if (args == null){
        throw new NullPointerException("PUTAIN DE MERDE");
    } else {
        System.out.println("Résultat du test :" + args.getInt("test", 0));
    }

    Tache t = (Tache) getArguments().getSerializable(LesTachesActivity.TACHE);
    setTache(t);

    durationView.setText(tache.getDuree() + R.string.mn);
    tittleView.setText(tache.getNom());
    descView.setText(tache.getDescription());

    Categorie cat = tache.getCategorie();

    switch (cat){
        case TRAVAIL:
            imgView.setImageResource(R.drawable.travail);
            break;
        case SPORT:
            imgView.setImageResource(R.drawable.sport);
            break;
        case MENAGE:
            imgView.setImageResource(R.drawable.menage);
            break;
        case LECTURE:
            imgView.setImageResource(R.drawable.lecture);
            break;
        case ENFANTS:
            imgView.setImageResource(R.drawable.enfant);
            break;
        case COURSES:
            imgView.setImageResource(R.drawable.courses);
            break;
        default:
            imgView.setImageResource(R.drawable.point_interro_);
    }
}

public void setTache(Tache t){
    this.tache = t;
}
}

这是我的布局。

detail_tache_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    tools:context=".DetailsActivity">
    <TextView
        android:id="@+id/taskTittle"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="22sp" />
    <TextView
        android:id="@+id/taskDuration"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/taskImg"
        android:layout_width="wrap_content"
        android:layout_height="200dp" />
    <TextView
        android:id="@+id/taskDescription"
        android:layout_marginTop="14dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"/>
</LinearLayout>

detail_tache_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/detail_fragment"
        android:name="griffern.android.tp3.DetailsFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

taches_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp">


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="TextView"
        android:textSize="23sp" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/fragment"
        android:name="griffern.android.tp3.LesTachesFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

0 个答案:

没有答案