Android运行时异常。无法开始活动

时间:2017-02-20 19:19:10

标签: android xml android-layout

我正在制作一个非常简单的壁纸制作工具。该应用程序在模拟器上工作正常,但在设备上不起作用。我也试过了其他设备上的应用程序,结果是一样的。

MainActivity.java:

public class MainActivity extends AppCompatActivity  {
    ImageView iv;
    FloatingActionButton fab,fab2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        setSupportActionBar(toolbar);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setImageResource(R.drawable.plus_24x24);
        fab.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select     Picture"), 9999);

            }
        });
        fab2=(FloatingActionButton)findViewById(R.id.fab2);
        fab2.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        fab2.setImageResource(R.drawable.ic_done_white);
        fab2.setVisibility(View.INVISIBLE);
        fab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WallpaperManager myWallpaperManager
                    = WallpaperManager.getInstance(getApplicationContext());
                try {
                    myWallpaperManager.setBitmap(((BitmapDrawable)iv.getDrawable()).getBitmap());
                    Snackbar.make(findViewById(android.R.id.content), "Wallpaper set", Snackbar.LENGTH_SHORT).show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        iv=(ImageView)findViewById(R.id.imageView);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 9999 && resultCode == RESULT_OK && data != null) {
            fab2.setVisibility(View.VISIBLE);
            Uri imageUri = data.getData();
            iv.setImageURI(imageUri);

       }
        else {
            Snackbar.make(findViewById(android.R.id.content), "No image selected", Snackbar.LENGTH_SHORT).show();
       }
   }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android.support.design="http://schemas.android.com/tools"
android:orientation="horizontal"
xmlns:app="http://schemas.android.com/apk/res-auto">


<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_marginTop="@dimen/ef_margin_medium"
    android:layout_marginLeft="@dimen/ef_margin_medium"
    android:layout_marginRight="@dimen/ef_margin_medium"
    android:layout_marginBottom="@dimen/ef_margin_medium"
    ads:cardCornerRadius="6dp"
    ads:cardElevation="6dp"
    >

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginEnd="@dimen/ef_margin_medium"
        android:layout_marginRight="@dimen/ef_margin_medium"
        android:layout_marginBottom="@dimen/ef_margin_medium"
        />

    <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true" />


</android.support.v7.widget.CardView>

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab2"
    android:layout_width="wrap_content"
    android:layout_height="51dp"
    app:fabSize="mini"
    android:backgroundTint="@color/colorAccent"
    android:layout_marginTop="34dp"
    android:layout_marginRight="11dp"
    android:layout_marginEnd="11dp"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/card_view"
    android:layout_alignEnd="@+id/card_view" />
</RelativeLayout>

错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.entripleaardee.incognitogallery, PID: 4571
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.entripleaardee.incognitogallery/com.entripleaardee.incognitogallery.MainActivity}: android.view.InflateException: Binary XML file line #55: Binary XML file line #55: Error inflating class <unknown>

2 个答案:

答案 0 :(得分:1)

您不能在一个文件中使用其他名称空间:

xmlns:ads

xmlns:app

xmlns:ads="http://schemas.android.com/apk/res-auto"代码中移除RelativeLayout,并将ads的每次出现替换为app

答案 1 :(得分:0)

只需将xmlns:ads替换为xmlns:app即可。喜欢 -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android.support.design="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">