我想从片段开始活动,但它会崩溃

时间:2018-02-08 05:35:39

标签: java android android-fragments

它是Android Studio的错误日志,它会崩溃。

02-08 10:56:28.746 9956-9956/lifeline.learn.com.slidingfragments E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: lifeline.learn.com.slidingfragments, PID: 9956
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{lifeline.learn.com.slidingfragments/lifeline.learn.com.slidingfragments.Main2Activity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.github.mikephil.charting.charts.PieChart
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                                       at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
                                                                                    Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.github.mikephil.charting.charts.PieChart
                                                                                    Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.github.mikephil.charting.charts.PieChart
                                                                                    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.github.mikephil.charting.charts.PieChart" on path: DexPathList[[zip file "/data/app/lifeline.learn.com.slidingfragments-1/base.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_dependencies_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_0_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_1_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_2_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_3_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_4_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_5_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_6_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_7_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_8_apk.apk", zip file "/data/app/lifeline.learn.com.slidingfragments-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/lifeline.learn.com.slidingfragments-1/lib/arm, /system/lib, /vendor/lib]]
                                                                                       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                       at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                                       at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                                       at android.view.LayoutInflater.createView(LayoutInflater.java:616)
                                                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
                                                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
                                                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
                                                                                       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
                                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
                                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
                                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
                                                                                       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                                                                                       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                       at lifeline.learn.com.slidingfragments.Main2Activity.onCreate(Main2Activity.java:23)
                                                                                       at android.app.Activity.performCreate(Activity.java:6956)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                                       at android.app.ActivityThread.-wrap14(ActivityThread.java)
02-08 10:56:28.746 9956-9956/lifeline.learn.com.slidingfragments E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

它的Tab 3我在片段中调用了开始活动:

public class Tab3 extends android.support.v4.app.Fragment {
    Context context;
    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance ();
    DatabaseReference databaseReference = firebaseDatabase.getReference ();
    private JazzyListView listView;
    ArrayAdapter<String> listAdapter;
    List<String> ListString = new ArrayList<> ();
    View view;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getContext ();
        view = inflater.inflate (R.layout.fragment_tab3, container, false);
        listView = (JazzyListView) view.findViewById (R.id.listview);
        ListString.add ("Adding");
        listAdapter = new ArrayAdapter<String> (context, android.R.layout.simple_list_item_1, ListString);
        final Connectivity connectivity = new Connectivity (context);
        if(connectivity.isConnected ()){
            databaseReference.addValueEventListener (new ValueEventListener () {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    ListString.remove ("Adding");
                    DataSnapshot snapshot = dataSnapshot.child ("User");
                    for(DataSnapshot s: snapshot.getChildren ())
                    {
                        if(s.getValue ().toString ().isEmpty ())
                            Toast.makeText (context, "Empty", Toast.LENGTH_SHORT).show ();
                        else{
                            ListString.add(s.getKey ().toString ());
                        }
                    }
                    listAdapter.notifyDataSetChanged ();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Toast.makeText (context, databaseError.getMessage (), Toast.LENGTH_SHORT).show ();
                }
            });
            listView.setAdapter (listAdapter);
            listView.setOnItemClickListener (new AdapterView.OnItemClickListener () {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String name = adapterView.getItemAtPosition (i).toString ();
                    Intent intent = new Intent (getActivity (), Main2Activity.class).putExtra("name", name);
                    try {
                        getActivity ().startActivity (intent);
                    }catch (Exception e){
                        Toast.makeText (context, e.getMessage (), Toast.LENGTH_SHORT).show ();
                    }
                }
            });
        }



        return view;
    }
}

这是我想要调用的Main2Activity.java,但每当我调用它时都会崩溃。

public class Main2Activity extends AppCompatActivity {

    int present =0,absent=0, late=0;
    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance ();
    DatabaseReference databaseReference = firebaseDatabase.getReference ();
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main2);
        String name = getIntent ().getStringExtra ("name");
        //pieChart = (PieChart) findViewById (R.id.pie);
        try {
            //pieChart.setCenterText ("Children");
            //pieChart.setCenterTextSize (10f);
            databaseReference.addValueEventListener (new ValueEventListener () {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    DataSnapshot snapshot = dataSnapshot.child ("Attendence");
                    for(DataSnapshot snapshot1 : snapshot.getChildren ()){
                        DataSnapshot snapshot2 = snapshot.child (snapshot1.getKey ());
                        for(DataSnapshot name: snapshot2.getChildren ()){
                            if(name.getKey ().equals (name)){
                                Log.e ("Got children","");
                            }
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }catch (Exception e){
            Toast.makeText (this, e.getMessage (), Toast.LENGTH_SHORT).show ();
        }
    }
}

每当我点击listview应用程序的项目时都会崩溃。我甚至试过try和catch方法来检测问题。我该怎么做才能调用活动并从应用程序中删除此崩溃。因为我是Android开发的新手。帮助我摆脱这个问题

2 个答案:

答案 0 :(得分:0)

您遇到此问题,因为Android无法找到您的外部库类com.github.mikephil.charting.charts.PieChart

在您的错误日志中,清楚地提到了它。

Binary XML file line #0: Error inflating class com.github.mikephil.charting.charts.PieChart
  • 首先,您必须清理并重建您的项目。如果问题已解决,请参阅。
  • 查看您图书馆的github文档,了解如何将其集成到Android中。

答案 1 :(得分:-1)

例外是关于Multidex。

总之, Android可以支持64K方法。 但是你使用的是很多库。 因此总方法的数量大于64K。 例外意味着它。

要解决此问题,您应该使用Multidex。 这并不困难。 您只需编辑清单文件build.gradle文件。

您可以找到更多信息:https://developer.android.com/studio/build/multidex.html