java.lang.RuntimeException,引起:android.view.InflateException

时间:2018-03-22 05:53:49

标签: java android xml

我在我的应用程序中获得以下错误代码主要是由Android版本4.4 i 我无法找到错误

您好我正在尝试构建电池警报应用程序,我不断收到java.lang.RuntimeException错误。我已经阅读了有关stackoverflow的一些主题,他们的AndroidManifest.xml似乎有些问题。我根本没有编辑该文件,但我仍然收到此错误。这是代码:

java.lang.RuntimeException: 
          at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2328)
          at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2386)
          at android.app.ActivityThread.access$900 (ActivityThread.java:169)
          at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1277)
          at android.os.Handler.dispatchMessage (Handler.java:102)
          at android.os.Looper.loop (Looper.java:136)
          at android.app.ActivityThread.main (ActivityThread.java:5476)
          at java.lang.reflect.Method.invokeNative (Native Method)
          at java.lang.reflect.Method.invoke (Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1283)
          at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1099)
          at dalvik.system.NativeStart.main (Native Method)
        Caused by: android.view.InflateException: 
          at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:719)
          at android.view.LayoutInflater.rInflate (LayoutInflater.java:761)
          at android.view.LayoutInflater.rInflate (LayoutInflater.java:769)
          at android.view.LayoutInflater.rInflate (LayoutInflater.java:769)
          at android.view.LayoutInflater.rInflate (LayoutInflater.java:769)
          at android.view.LayoutInflater.rInflate (LayoutInflater.java:769)
          at android.view.LayoutInflater.inflate (LayoutInflater.java:498)
          at android.view.LayoutInflater.inflate (LayoutInflater.java:398)
          at android.view.LayoutInflater.inflate (LayoutInflater.java:354)
          at android.support.v7.app.AppCompatDelegateImplV9.setContentView (AppCompatDelegateImplV9.java:288)
          at android.support.v7.app.AppCompatActivity.setContentView (AppCompatActivity.java:140)
          at www.androidghost.com.batteryalarm.MainActivity.onCreate (MainActivity.java:232)
          at android.app.Activity.performCreate (Activity.java:5451)
          at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1093)
          at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2292)
        Caused by: android.content.res.Resources$NotFoundException: 
          at android.content.res.Resources.loadDrawable (Resources.java:3063)
          at android.content.res.TypedArray.getDrawable (TypedArray.java:602)
          at android.view.View.<init> (View.java:3687)
          at android.widget.TextView.<init> (TextView.java:913)
          at android.widget.Button.<init> (Button.java:108)
          at android.support.v7.widget.AppCompatButton.<init> (AppCompatButton.java:66)
          at android.support.v7.widget.AppCompatButton.<init> (AppCompatButton.java:62)
          at android.support.v7.app.AppCompatViewInflater.createView (AppCompatViewInflater.java:109)
          at android.support.v7.app.AppCompatDelegateImplV9.createView (AppCompatDelegateImplV9.java:1021)
          at android.support.v7.app.AppCompatDelegateImplV9.onCreateView (AppCompatDelegateImplV9.java:1080)
          at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView (LayoutInflaterCompatHC.java:47)
          at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:690)
        Caused by: org.xmlpull.v1.XmlPullParserException: 
          at android.graphics.drawable.Drawable.createFromXmlInner (Drawable.java:986)
          at android.graphics.drawable.Drawable.createFromXml (Drawable.java:930)
          at android.content.res.Resources.loadDrawable (Resources.java:3059)

这是我的MainActivit代码

 public class MainActivity extends AppCompatActivity
  {

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

                linearLayout= (LinearLayout) findViewById(R.id.relative_main);
                btnTechnology = (Button) findViewById(R.id.btnTechnology);
                btnHealth = (Button) findViewById(R.id.btnHealth);
                btnStop=(Button) findViewById(R.id.btnStop);
                btnPower = (Button) findViewById(R.id.btnPower);
                btnTemp = (Button) findViewById(R.id.btntemp);
                btnVoltage = (Button) findViewById(R.id.btnVoltage);
                btnFab= (FloatingActionButton) findViewById(R.id.btnfab);
                layoutButton= (LinearLayout) findViewById(R.id.layoutButton);
                layoutContent= (RelativeLayout) findViewById(R.id.layoutContent);
                btnCapacity= (Button) findViewById(R.id.btnCapacity);
                toolbar = (Toolbar) findViewById(R.id.toolbar_id);

                setSupportActionBar(toolbar);
                try
                {
                    actionBar=getSupportActionBar();
                    actionBar.setHomeAsUpIndicator(R.mipmap.home);
                    actionBar.setDisplayHomeAsUpEnabled(true);
                    actionBar.setHomeButtonEnabled(true);
                }
                catch (NullPointerException e)
                {
                    e.printStackTrace();
                }


                mWaveLoadingView = (WaveLoadingView) findViewById(R.id.waveLoadingView);
                mWaveLoadingView.setShapeType(WaveLoadingView.ShapeType.CIRCLE);
                mWaveLoadingView.setCenterTitleColor(Color.BLACK);
                mWaveLoadingView.setBottomTitleSize(20);
                mWaveLoadingView.setBorderWidth(4);
                mWaveLoadingView.setAmplitudeRatio(60);
                mWaveLoadingView.setAnimDuration(3000);
                mWaveLoadingView.pauseAnimation();
                mWaveLoadingView.resumeAnimation();
                mWaveLoadingView.cancelAnimation();
                mWaveLoadingView.startAnimation();


                try
                {
                    btnStop.setText("Stop Alarm");
                    btnStop.setTextColor(Color.parseColor("#FFAF22DA"));
                    Toast.makeText(this, "Alarm Activated", Toast.LENGTH_SHORT).show();

                    startService(new Intent(this, RegisterAlarmService.class));
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }


                //fetch value from firebase to use check for update
                try
                {
                    // 1 Create the singleton Remote Config object
                      mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();

                      // To fetch parameter values from the Remote Config Server
                      long cacheExpiration=0;
                      mFirebaseRemoteConfig.fetch(cacheExpiration)
                        .addOnCompleteListener(this, new OnCompleteListener<Void>()
                    {
                        @Override
                        public void onComplete(@NonNull Task<Void> task)
                        {
                            if (task.isSuccessful())
                            {
                                // Once the config is successfully fetched it must be activated before newly fetched
                                // values are returned.
                                mFirebaseRemoteConfig.activateFetched();
                            }
                            displayDialogMessage();
                        }
                    });

                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }


             try
             {
                 //wheather to show AutoStart dialog or not
                 mSharedPreferencesPermission=getSharedPreferences("permission",Context.MODE_PRIVATE);
                 Cancel_value= mSharedPreferencesPermission.getString("autostart","Def_value");

                 if(Cancel_value.equals("Def_value"))
                 {
                     if(Build.BRAND.equalsIgnoreCase("xiaomi") || Build.BRAND.equalsIgnoreCase("Letv") || Build.BRAND.equalsIgnoreCase("Honor")|| Build.BRAND.equalsIgnoreCase("oppo") || Build.BRAND.equalsIgnoreCase("vivo"))
                     {
                         mAutoPermissionDialog.show(getFragmentManager(),"My Permission");

                     }
                 }

             }
             catch (Exception e)
             {
                 e.printStackTrace();
             }


                mSpringMenu = new SpringMenu(this, R.layout.view_menu);
                mSpringMenu.setFadeEnable(true);
                mSpringMenu.setChildSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(20, 5));
                mSpringMenu.setDragOffset(0.10f);
                mSpringMenu.setDirection(0);          //0 For Left and 1 For Right

                mListView= (ListView) findViewById(R.id.nev_listView);
                MyAdapter adapter=new MyAdapter(this,title,image);
                mListView.setAdapter(adapter);
                ListItemListener listener=new ListItemListener(this,getSupportFragmentManager());
                mListView.setOnItemClickListener(listener);


                try
                {
                    //get the value of checkbox to check the rate dialog will show in future or not

                    SharedPreferences Ratepreferences=getSharedPreferences("Rates", Context.MODE_PRIVATE);
                    boolean check= Ratepreferences.getBoolean("Rate",false);   //default value is false means show dialog

                    SimpleDateFormat dateFormat = new SimpleDateFormat("dd");    //get the system curent date
                    String compareDate = "15";            //assign the date at which dialog will appear

                    Date date = new Date();
                    String getDate = dateFormat.format(date);            //convert the system date to string

                    if(!check)        //check wheather the checkbox is selected or not
                    {
                        if (getDate.equals(compareDate))                 //compare the date
                        {
                            RateMeDialog rateMeDialog=new RateMeDialog();                  //if all condition meats then show dialog
                            rateMeDialog.show(getFragmentManager(),"My Rate");
                        }
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }

            @Override
            protected void onResume()
            {
                mRewardedVideoAd.resume(this);
                registerReceiver(myBroadCast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

                try
                {
                    mSharedPreferencesTheft_Value=getSharedPreferences("theft_value", Context.MODE_PRIVATE);
                    Theft_Set_Value=mSharedPreferencesTheft_Value.getString("thefts_value","Theft Alarm Off");

                    mSharedPreferencesTheft_Image=getSharedPreferences("theft_image",Context.MODE_PRIVATE);
                    Theft_Set_Image=mSharedPreferencesTheft_Image.getInt("thefts_image",R.mipmap.theft_off);

                    btnTheft= (Button) findViewById(R.id.btnTheft);

                    if (Theft_Set_Value.equals("Theft Alarm Off"))
                    {
                        btnTheft.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.theft_off, 0, 0);
                        btnTheft.setText(Theft_Set_Value);
                        btnTheft.setTextColor(Color.parseColor("#9BA1A8"));
                    }
                    else
                    {
                        btnTheft.setCompoundDrawablesWithIntrinsicBounds(0,Theft_Set_Image, 0, 0);
                        btnTheft.setText(Theft_Set_Value);
                        btnTheft.setTextColor(Color.parseColor("#FFAF22DA"));
                    }

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }


                super.onResume();

            }

            @Override
            public void onPause()
            {
                mRewardedVideoAd.pause(this);
                super.onPause();
            }

            @Override
            protected void onDestroy()
            {
                mRewardedVideoAd.destroy(this);
                unregisterReceiver(myBroadCast);
                super.onDestroy();
            }



            @Override
            public boolean dispatchTouchEvent(MotionEvent ev)
            {

                return mSpringMenu.dispatchTouchEvent(ev);

            }



            @Override
            public boolean onCreateOptionsMenu(Menu menu)
            {
                getMenuInflater().inflate(R.menu.menu_main, menu);
                return super.onCreateOptionsMenu(menu);
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item)
            {

                if(item.getItemId()==R.id.navigate)
                {
                    DialogInfo dialogInfo=new DialogInfo();
                    dialogInfo.show(getFragmentManager(), "My Info");
                }
                else if(item.getItemId()==android.R.id.home)
                {
                   mSpringMenu.openMenu();
                }
                return super.onOptionsItemSelected(item);

            }







            private void displayDialogMessage()
            {
                String getVersionCode= mFirebaseRemoteConfig.getString("android_update_version_code");
                try
                {
                    FireBaseVersionCode=Integer.valueOf(getVersionCode);

                }
                catch (NumberFormatException e)
                {
                    e.printStackTrace();
                }


                //Get PackageInfo like VersionCode
                PackageManager packageManager=this.getPackageManager();

                try
                {

                    PackageInfo info=packageManager.getPackageInfo(this.getPackageName(),0);
                    VersionCode=info.versionCode;
                }
                catch (PackageManager.NameNotFoundException e)
                {
                    e.printStackTrace();
                }

                if(VersionCode<FireBaseVersionCode)
                {
                    mUpdateDialog.show(getSupportFragmentManager(),"My Update");
                }


            }





            @Override
            public void onBackPressed()
            {
                if(mBackPressed + time > System.currentTimeMillis())
                {
                    super.onBackPressed();
                    return;
                }
                else
                {
                    Snackbar snackbar=Snackbar.make(linearLayout,"Confirm Exit",Snackbar.LENGTH_LONG);
                    View view2=snackbar.getView();
                    view2.setBackgroundColor(Color.parseColor("#FFAF22DA"));
                    TextView textView= (TextView) view2.findViewById(android.support.design.R.id.snackbar_text);
                    textView.setTextSize(20);
                    textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                    snackbar.show();
                    mBackPressed=System.currentTimeMillis();
                }

            }



            public void Stop(View v)
            {

               if (btnStop.getText().toString().equals("Stop Alarm"))
                {
                    btnStop.setText("Start Alarm");

                    Snackbar snackbar=Snackbar.make(linearLayout,"Alarm Deactivated",Snackbar.LENGTH_LONG);
                    View view2=snackbar.getView();
                    view2.setBackgroundColor(Color.parseColor("#FFAF22DA"));
                    TextView textView= (TextView) view2.findViewById(android.support.design.R.id.snackbar_text);
                    textView.setTextSize(20);
                    textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                    snackbar.show();

                    btnStop.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.alarm_start, 0, 0);
                    btnStop.setTextColor(Color.parseColor("#9BA1A8"));

                    stopService(new Intent(this, MyAlarmServiceClass.class));
                   stopService(new Intent(this, RegisterAlarmService.class));

                }
                else
                    {
        //this else block will execute only when toggle button is off in setting now here we will on toggle button

                        btnStop.setText("Stop Alarm");

                        Snackbar snackbar=Snackbar.make(linearLayout,"Alarm Activated",Snackbar.LENGTH_LONG);
                        View view3=snackbar.getView();
                        view3.setBackgroundColor(Color.parseColor("#FFAF22DA"));
                        TextView textView= (TextView) view3.findViewById(android.support.design.R.id.snackbar_text);
                        textView.setTextSize(20);
                        textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                        snackbar.show();

                        btnStop.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.alarm_stop, 0, 0);
                        btnStop.setTextColor(Color.parseColor("#FFAF22DA"));


                        startService(new Intent(this, RegisterAlarmService.class));

                     }

            }



        public void ThetAlarmBtn(View view)
        {

            mSharedPreferencesTheft_Value=getSharedPreferences("theft_value", Context.MODE_PRIVATE);
            SharedPreferences.Editor mEditor=mSharedPreferencesTheft_Value.edit();

            mSharedPreferencesTheft_Image=getSharedPreferences("theft_image",Context.MODE_PRIVATE);
            SharedPreferences.Editor editor_image=mSharedPreferencesTheft_Image.edit();



                    if (btnTheft.getText().toString().equals("Theft Alarm Off"))
                    {
                        if(status==BatteryManager.BATTERY_STATUS_CHARGING)
                        {
                        btnTheft.setText("Theft Alarm On");

                            Snackbar snackbar=Snackbar.make(linearLayout,"Theft Alarm On",Snackbar.LENGTH_LONG);
                            View view4=snackbar.getView();
                            view4.setBackgroundColor(Color.parseColor("#FFAF22DA"));
                            TextView textView= (TextView) view4.findViewById(android.support.design.R.id.snackbar_text);
                            textView.setTextSize(20);
                            textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                            snackbar.show();

                        btnTheft.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.theft_on, 0, 0);
                       btnTheft.setTextColor(Color.parseColor("#FFAF22DA"));


                        Theft_Get_Value = "Theft Alarm On";
                        mEditor.putString("thefts_value", Theft_Get_Value);
                        mEditor.apply();

                        Theft_Get_Image = R.mipmap.theft_on;
                        editor_image.putInt("thefts_image", Theft_Get_Image);
                        editor_image.apply();


                        startService(new Intent(this, RegisterTheftService.class));
                    }
                        else if(status==BatteryManager.BATTERY_STATUS_DISCHARGING)
                        {

                            Snackbar snackbar=Snackbar.make(linearLayout,"Please Connect Your Device To Charger To Activate Theft Alarm",Snackbar.LENGTH_LONG);
                            View view4=snackbar.getView();
                            view4.setBackgroundColor(Color.parseColor("#FFAF22DA"));
                            snackbar.show();
                        }
                }

                    else
                    {
                        btnTheft.setText("Theft Alarm Off");

                        Snackbar snackbar=Snackbar.make(linearLayout,"Theft Alarm Off",Snackbar.LENGTH_LONG);
                        View view5=snackbar.getView();
                        view5.setBackgroundColor(Color.parseColor("#FFAF22DA"));
                        TextView textView= (TextView) view5.findViewById(android.support.design.R.id.snackbar_text);
                        textView.setTextSize(20);
                        textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                        snackbar.show();

                        btnTheft.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.theft_off, 0, 0);
                        btnTheft.setTextColor(Color.parseColor("#9BA1A8"));


                        Theft_Get_Value = "Theft Alarm Off";
                        mEditor.putString("thefts_value", Theft_Get_Value);
                        mEditor.apply();

                        Theft_Get_Image = R.mipmap.theft_off;
                        editor_image.putInt("thefts_image", Theft_Get_Image);
                        editor_image.apply();


                        stopService(new Intent(this, RegisterTheftService.class));
                        stopService(new Intent(this, MyTheftServiceClass.class));

                    }
        }


            public void OpenInfo(View v)
            {
                if(!isopen)
                {
                    int x=layoutContent.getRight();
                    int y=layoutContent.getBottom();
                    int startRadius=0;
                    int endRadius= (int) Math.hypot(linearLayout.getWidth(),linearLayout.getHeight());
                   btnFab.setImageResource(R.mipmap.menu_close);

                    Animator animator= ViewAnimationUtils.createCircularReveal(layoutButton,x,y,startRadius,endRadius);
                    layoutButton.setVisibility(View.VISIBLE);
                    animator.start();

                    isopen=true;

                }
                else
                {
                    int x=layoutContent.getRight();
                    int y=layoutContent.getBottom();
                    int startRadius=Math.max(linearLayout.getWidth(),linearLayout.getHeight());
                    int endRadius= 0;
                    btnFab.setImageResource(R.mipmap.menu_open);

                    Animator animator= ViewAnimationUtils.createCircularReveal(layoutButton,x,y,startRadius,endRadius);
                    layoutButton.setVisibility(View.VISIBLE);
                    animator.addListener(new Animator.AnimatorListener()
                    {
                        @Override
                        public void onAnimationStart(Animator animator) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animator)
                        {
                            layoutButton.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationCancel(Animator animator) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animator) {

                        }
                    });
                        animator.start();
                        isopen=false;
                }
            }

        }

这是我的xml代码

<include
    android:id="@+id/toolbar_id"
    layout="@layout/tool_bar">
</include>

<www.androidghost.com.batteryalarm.ArcLayout
    android:id="@+id/arcLayout"
    android:layout_weight="2.0"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:arc_cropDirection="cropOutside"
    android:elevation="10dp"
    app:arc_position="bottom"
    app:arc_height="50dp">

    <ImageView
        android:background="@drawable/background"/>

                <Button
                    android:drawableLeft="@drawable/temp2"
                    android:background="@drawable/ripple_effect/>

                <Button
                    android:drawableLeft="@drawable/voltage"
                    android:background="@drawable/ripple_effect/>

                <Button
                    android:drawableLeft="@drawable/capacity"
                    android:background="@drawable/ripple_effect"/>

                <Button
                    android:background="@drawable/ripple_effect"/>
                <Button
                    android:background="@drawable/ripple_effect/>

                <Button
                    android:drawableLeft="@drawable/technology"
                    android:background="@drawable/ripple_effect"/>
</www.androidghost.com.batteryalarm.ArcLayout>

    <Button
        android:background="@drawable/dialog_ripple"/>

    <android.support.design.widget.FloatingActionButton
        android:src="@mipmap/menu_open"
    </android.support.design.widget.FloatingActionButton>

    <Button
        android:drawableTop="@mipmap/alarm_stop"
        android:background="@drawable/dialog_ripple"/>

3 个答案:

答案 0 :(得分:0)

如果您使用 AppCompat 23.2.0 ,则需要设置这些标记。

如果你使用的是Gradle插件的v2.0或更高版本,我们有一个方便的快捷方式来启用所有内容:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

如果您尚未更新,并且正在使用Gradle插件的v1.5.0或更低版本,则需要将以下内容添加到应用的build.gradle中:

android {
  defaultConfig {
    // Stops the Gradle plugin’s automatic rasterization of vectors
    generatedDensities = []
  }

  // Flag to tell aapt to keep the attribute ids around
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}

如果未启用此标记,则在运行 KitKat或更低版本的设备上运行应用时,您会看到此(或类似)错误。

## OR ##

您还可以升级到 23.4.0

dependencies {
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

您需要编写以下代码,以便在使用向量的Activity顶部启用此功能:

static {
    AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
}

在精彩文章AppCompat — Age of the vectors

上查看有关此内容的更多详细信息

答案 1 :(得分:0)

最后我发现我的问题的解决方案我在我的代码中使用了涟漪效应 删除涟漪效应xml文件后,该应用程序运行良好的kitkat版本 但我不知道为什么我不能在kitkat版本中使用涟漪效应..

答案 2 :(得分:-1)

确保所有ID都正确并且在xml布局中可用,并检查是否有任何从drawble加载的资源可用,即 如果drawble-v21中有一个文件,那么它也应该可以在drawable文件夹中找到。 有关更多信息,请按照此Android resource not found exception?

进行操作