应用程序在启动另一个活动时崩溃

时间:2017-10-01 03:44:52

标签: android listview fragment

我正在尝试制作一个在不同屏幕尺寸上运行不同的小应用程序,该应用程序在大屏幕上运行完美,但在从一个活动移动到另一个活动时它在普通手机上崩溃,所以我将发布正常大小的代码,在应用程序中,第一个活动应该是main(mainactivity.java),它显示一个列表视图(Workoutlist.java),当我点击它时应该启动另一个显示片段的活动(Workdetail2),但它崩溃而不是打开另一个活动

这是 mainactivity.java

package com.hfad.workout;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

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

  public void clickme(long click) {
    Workdetail v = (Workdetail) 
getFragmentManager().findFragmentById(R.id.detail);

    if (v != null) {
        Workdetail wd = (Workdetail) 
getFragmentManager().findFragmentById(R.id.detail);

        wd.setwork((long) click);

    } else {

        Intent in = new Intent(this, Workdetail2.class);
        in.putExtra("hello", click);
        startActivity(in);


        Workdetail2 w2 = new Workdetail2();
        w2.onme(click);



    }
  }
}

WorkList.java

 public class WorkoutList extends ListFragment {

    MainActivity ma;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        ArrayAdapter<Workout> arrayAdapter = new ArrayAdapter<Workout>(
                inflater.getContext(), android.R.layout.simple_list_item_1, 
   Workout.workout);
        setListAdapter(arrayAdapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }
    @Override
    public void onAttach(Context ac){
        super.onAttach(ac);
        this.ma =(MainActivity) getActivity();

    }

    public void onListItemClick(ListView v, View vi, int position, long id){
        ma.clickme(id);

    }
}

Workdetail2.java

public class Workdetail2 extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_workdetail2);
    Intent in = getIntent();
    long ln= in.getLongExtra("hello",0);

    Workdetail wd = (Workdetail) 
    getFragmentManager().findFragmentById(R.id.detailss);
    wd.setwork(ln);

  }
} 

Workdetail.java

 public class Workdetail extends Fragment {

   private  long work;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
     // Inflate the layout for this fragment

     return inflater.inflate(R.layout.detail_work, container, false);

   }

   public void onStart() {
     super.onStart();
     View view = getView();
     if (view != null) {
        TextView tx1 = (TextView) view.findViewById(R.id.first);
        Workout wk = Workout.workout[(int) work];
        tx1.setText(wk.getWorkoutname());
        TextView tx2 = (TextView) view.findViewById(R.id.detail2);
        tx2.setText(wk.getDesc());
     }

   }

   public void setwork(long workn){
     this.work= workn;
     View view = getView();
     if (view != null) {
        TextView tx1 = (TextView) view.findViewById(R.id.first);
        Workout wk = Workout.workout[(int) work];
        tx1.setText(wk.getWorkoutname());
        TextView tx2 = (TextView) view.findViewById(R.id.detail2);
        tx2.setText(wk.getDesc());
     }
   }
}

Workout.java

public class Workout {
  private String workoutname;
  private String desc;

   public static final Workout[]  workout= {new Workout("power","100 Headstand pushup \n 50 1-legged squats\n 30 pullups"),new Workout("energy","5 -10 metre sprint\n 3 20-pushup"),new Workout("strength","3 20-pullups \n 3 20-pullups")};

  private Workout(String workoutname,String desc){
    this.workoutname = workoutname;
    this.desc = desc;

  }

  public String getWorkoutname() {
    return workoutname;
  }

  public  String getDesc(){
    return desc;
  }

  public String toString() {
    return workoutname;
  }

}

我没有遇到问题,如果有人可以告诉我哪里错了,那将是一个很大的帮助。

错误

--------- beginning of crash


 FATAL EXCEPTION: main Process: com.hfad.workout, PID: 7292


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hfad.workout/com.hfad.workout.Workdetail2}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
     at android.app.ActivityThread.-wrap12(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6119)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f04001d
     at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2101)
     at android.content.res.Resources.getLayout(Resources.java:1115)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:424)
     at com.hfad.workout.Workdetail.onCreateView(Workdetail.java:28)
     at android.app.Fragment.performCreateView(Fragment.java:2353)
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:959)
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1149)
     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1251)
     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2311)
     at android.app.FragmentController.onCreateView(FragmentController.java:98)
     at android.app.Activity.onCreateView(Activity.java:5901)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
     at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
     at android.app.Activity.setContentView(Activity.java:2414)
     at com.hfad.workout.Workdetail2.onCreate(Workdetail2.java:16)
     at android.app.Activity.performCreate(Activity.java:6679)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
     at android.app.ActivityThread.-wrap12(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6119)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

更新 现在这是我猜的崩溃的真正错误

2 个答案:

答案 0 :(得分:1)

从您粘贴的错误日志中我可以告诉您,您在R.layout.activity_workdetail2的XML文件中出现了一些错误 如果您粘贴布局文件,我们将能够为您提供帮助。

答案 1 :(得分:1)

在片段中使用它

 View rootView = null;
    Context context;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
        }
        View rootView = inflater.inflate(R.layout.expert_fragment_details, container, false);
        context = getActivity();

        return rootView;
    }