有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAlang.RuntimeException,由以下原因引起:安卓。看法膨胀异常

我在我的应用程序中得到以下错误代码,主要是安卓版本4.4i 我找不到错误

嗨,我正在尝试建立一个电池报警应用程序,我一直在得到一个java。lang.RuntimeException错误。我读过一些关于stackoverflow的主题,它们的问题似乎与AndroidManifest有关。xml。我根本没有编辑那个文件,但我仍然得到这个错误。代码如下:

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

这是我的维护代码

 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()==安卓.R.id.home)
                {
                   mSpringMenu.openMenu();
                }
                return super.onOptionsItemSelected(item);

            }







            private void displayDialogMessage()
            {
                String getVersionCode= mFirebaseRemoteConfig.getString("安卓_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(安卓.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(安卓.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(安卓.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(安卓.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(安卓.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
    安卓:id="@+id/toolbar_id"
    layout="@layout/tool_bar">
</include>

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

    <ImageView
        安卓:background="@drawable/background"/>

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

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

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

                <Button
                    安卓:background="@drawable/ripple_effect"/>
                <Button
                    安卓:background="@drawable/ripple_effect/>

                <Button
                    安卓:drawableLeft="@drawable/technology"
                    安卓:background="@drawable/ripple_effect"/>
</www.安卓ghost.com.batteryalarm.ArcLayout>

    <Button
        安卓:background="@drawable/dialog_ripple"/>

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

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


共 (3) 个答案

  1. # 1 楼答案

    确保所有ID都正确并且在xml布局中可用,同时检查从drawble加载的任何资源是否可用,即 如果文件在drawble-v21中可用,则它也应在drawable文件夹中可用。 更多信息请参见此Android resource not found exception?

  2. # 2 楼答案

    如果使用的是AppCompat 23.2.0,则需要设置这些标志

    如果您使用的是v2。0或更高版本的Gradle插件,我们有一个方便的快捷方式来启用一切:

    android {
      defaultConfig {
        vectorDrawables.useSupportLibrary = true
      }
    }
    

    如果您尚未更新,并且正在使用v1。5.0或更低版本的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或更低版本的设备上运行应用程序时,您将看到此(或类似)错误

    ###或##

    您还可以升级到23.4.0

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

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

    static {
        AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
    }
    

    在很棒的文章AppCompat — Age of the vectors上查看更多关于这方面的详细信息

  3. # 3 楼答案

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