2017-02-20 57 views
0

我正在處理一個非常簡單的壁紙設置。該應用程序在模擬器上正常工作,但不適用於設備。我也嘗試過其他設備上的應用程序,結果是一樣的。Android運行時異常。無法啓動活動

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> 
+1

請提供您的日誌消息中接下來的幾個字符串。 – MaxV

+0

大概顯示你的清單文件。它有''? –

+0

您在上的#55行有錯誤。似乎你沒有支持庫。我相信FAB在com.android.support:design中。你可以將「compile」com.android.support:design:25.0.1'「添加到你的gradle dependencies中 –

回答

1

你不能有額外的命名空間中的一個文件:

xmlns:ads 

xmlns:app 

RelativeLayout從標籤移除xmlns:ads="http://schemas.android.com/apk/res-auto"並用app取代的ads每次出現。

+0

非常感謝你,先生,它工作。 –

+0

這是網站,您可以從中獲得有關程序性問題的答案。但要知道答案對其他人是否有價值,它必須被標記爲已解決並且可能給出了要點。您可以通過在我的答案旁邊打上標記符號並提供優惠,爲本網站的發展做出貢獻。 –

0

只需更換您的xmlns:adsxmlns: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"> 
相關問題