关闭

Android Studio 2.2 preview 1 EditText error

标签: EditView
249人阅读 评论(0) 收藏 举报
分类:

使用Android Studio 2.2 preview 1版本时,遇到一个错误,新建一个activity时,只在布局文件中加一个EditView,运行程序就会出错;下面的内容是昨天写代码时遇到的;

05-31 16:56:39.696 9324-9324/com.example.----.phonesafe E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example..----..phonesafe, PID: 9324
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example..----..phonesafe/com.example..----..phonesafe.AddressActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class EditText
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:167)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1419)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                  at android.os.Looper.loop(Looper.java:194)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5546)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
                                                                               Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class EditText
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:767)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
                                                                                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
                                                                                  at com.example.zhangtao.phonesafe.AddressActivity.onCreate(AddressActivity.java:29)
                                                                                  at android.app.Activity.performCreate(Activity.java:5977)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:167) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1419) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                  at android.os.Looper.loop(Looper.java:194) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5546) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 
                                                                               Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v21/abc_edit_text_material.xml from drawable resource ID #0x7f020015
                                                                                  at android.content.res.Resources.loadDrawableForCookie(Resources.java:2717)
                                                                                  at android.content.res.Resources.loadDrawable(Resources.java:2597)
                                                                                  at android.content.res.TypedArray.getDrawable(TypedArray.java:751)
                                                                                  at android.view.View.<init>(View.java:3777)
                                                                                  at android.widget.TextView.<init>(TextView.java:656)
                                                                                  at android.widget.EditText.<init>(EditText.java:70)
                                                                                  at android.widget.EditText.<init>(EditText.java:66)
                                                                                  at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60)
                                                                                  at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:56)
                                                                                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:963)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1022)
                                                                                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:729)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:810) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:508) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267) 
                                                                                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129) 
                                                                                  at com.example..----..phonesafe.AddressActivity.onCreate(AddressActivity.java:29) 
                                                                                  at android.app.Activity.performCreate(Activity.java:5977) 
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415) 
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:167) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1419) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                  at android.os.Looper.loop(Looper.java:194) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5546) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 
                                                                               Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #24: <nine-patch> requires a valid 9-patch source image
                                                                                  at android.graphics.drawable.NinePatchDrawable.updateStateFromTypedArray(NinePatchDrawable.java:448)
                                                                                  at android.graphics.drawable.NinePatchDrawable.inflate(NinePatchDrawable.java:404)
                                                                                  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1102)
                                                                                  at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:185)
                                                                                  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1102)
                                                                                  at android.graphics.drawable.InsetDrawable.inflate(InsetDrawable.java:104)
                                                                                  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1102)
                                                                                  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1024)
                                                                                  at android.content.res.Resources.createFromXml(Resources.java:3028)
                                                                                at android.content.res.Resources

上面是出错的信息;
从出错的信息中可以看到,直接关联的信息是这个

   at com.example.phonesafe.AddressActivity.onCreate(AddressActivity.java:29) 

找到相关的类;
下面贴出activity源码:

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.phonesafe.db.dao.AddressDao;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class AddressActivity extends AppCompatActivity {

    @ViewInject(R.id.et_query_num)
    private EditText queryNumEt;
    @ViewInject(R.id.btn_query)
    private Button queryBtn;
    @ViewInject(R.id.tv_show_address)
    private TextView showAddressTv;

    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        //设置activity的视图内容
        TextView titleBar = (TextView) findViewById(R.id.title_bar);
        titleBar.setText("手机号码归属地查询");
        //用xUtils注入
        ViewUtils.inject(this);
        queryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String queryNum = queryNumEt.getText().toString().trim();
                String queryAdress = AddressDao.queryAddress(context, queryNum);
                if (!TextUtils.isEmpty(queryAdress)){
                    showAddressTv.setText(queryAdress);
                }
            }
        });
    }
}

根据指引
at com.example.phonesafe.AddressActivity.onCreate(AddressActivity.java:29)

找到setContentView(R.layout.activity_address);
可以看出应该在activity_address.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"
    tools:context="com.example.phonesafe.AddressActivity">
    <include layout="@layout/title_bar"/>
    <EditText
        android:id="@+id/et_query_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="手机号码"/>
    <Button
        android:id="@+id/btn_query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="查询"/>
    <TextView
        android:id="@+id/tv_show_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"/>
</LinearLayout>

其中的title_bar.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="wrap_content">

    <TextView
        android:id="@+id/title_bar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colortitle"
        android:gravity="center"
        android:text="标题栏"
        android:textSize="22sp"/>

</LinearLayout>

好吧看到上面的还是找不到哦解决问题,其实上面的没问题,有问题的是这个Android Studio 2.2 preview 1 版本有问题,与EditView冲突
下面附上我的工程的build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0-alpha1'
//        classpath 'com.github.dcendents:android-maven-plugin:1.2'
//        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
//        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

将其中的gradle的版本从 classpath ‘com.android.tools.build:gradle:2.2.0-alpha1’ 换成这个classpath ‘com.android.tools.build:gradle:2.1.0’

更改之后project的build.gradle的内容是如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
//        classpath 'com.github.dcendents:android-maven-plugin:1.2'
//        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
//        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

另外附上stackoverflow 上对次问题解决的地址EditView error 解决办法

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:978次
    • 积分:70
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类