2017-07-25 52 views
1

我一直按步驟this tutorial,但是當我想加載配置文件圖像和標題背景沒有顯示。我添加了互聯網許可和wifi已打開並初始化drawerimageloader。問題是什麼?無法從URL加載配置文件圖像和標題背景

主要活動:

import android.net.Uri; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.ImageView; 

import com.mikepenz.materialdrawer.DrawerBuilder; 
import com.mikepenz.materialdrawer.AccountHeader; 
import com.mikepenz.materialdrawer.AccountHeaderBuilder; 
import com.mikepenz.materialdrawer.model.ProfileDrawerItem; 
import com.mikepenz.materialdrawer.model.interfaces.IProfile; 

import com.bumptech.glide.Glide; 


public class MainActivity extends AppCompatActivity {; 

Toolbar toolbar; 

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

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    new DrawerBuilder().withActivity(this).build(); 

    AccountHeader headerResult = new AccountHeaderBuilder() 
      .withActivity(this) 
      .addProfiles(
        new ProfileDrawerItem().withName("Mike 
    Penz").withEmail("[email protected]").withIcon(Uri. 
    parse("https://avatars3.githubusercontent.com/u/887462?v=3&s=460")) 
      ) 
      .withOnAccountHeaderListener(new 
    AccountHeader.OnAccountHeaderListener() { 
       @Override 
       public boolean onProfileChanged(View view, IProfile profile, 
    boolean currentProfile) { 
        return false; 
       } 
      }) 
      .build(); 

    new DrawerBuilder() 
      .withActivity(this) 
      .withAccountHeader(headerResult) 
      .build(); 

    ImageView view = headerResult.getHeaderBackgroundView(); 
    Glide.with(this).load("www.api.androidhive.info/images/nav-menu-header- 
    bg.jpg").into(view); 
    } 

    } 

自定義活動:

import android.app.Application; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.net.Uri; 
import android.widget.ImageView; 

import com.bumptech.glide.Glide; 
import com.mikepenz.iconics.IconicsDrawable; 
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader; 
import com.mikepenz.materialdrawer.util.DrawerImageLoader; 
import com.mikepenz.materialdrawer.util.DrawerUIUtils; 

/** 
* Created by mikepenz on 27.03.15. 
* 
*/ 
public class CustomApplication extends Application { 

@Override 
public void onCreate() { 
    super.onCreate(); 

    /* 
    //initialize and create the image loader logic 
    DrawerImageLoader.init(new AbstractDrawerImageLoader() { 
     @Override 
     public void set(ImageView imageView, Uri uri, Drawable placeholder) 
    { 

    Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder) 
    .into(imageView); 
     } 
     @Override 
     public void cancel(ImageView imageView) { 
      Picasso.with(imageView.getContext()).cancelRequest(imageView); 
     } 
    }); 
    */ 

    //initialize and create the image loader logic 
    DrawerImageLoader.init(new AbstractDrawerImageLoader() { 
     @Override 
     public void set(ImageView imageView, Uri uri, Drawable placeholder, 
    String tag) { 
    Glide.with(imageView.getContext()).load(uri) 
    .placeholder(placeholder).into(imageView); 
     } 

     @Override 
     public void cancel(ImageView imageView) { 
      Glide.clear(imageView); 
     } 

     @Override 
     public Drawable placeholder(Context ctx, String tag) { 
      //define different placeholders for different imageView targets 
      //default tags are accessible via the DrawerImageLoader.Tags 
      if (DrawerImageLoader.Tags.PROFILE.name().equals(tag)) { 
       return DrawerUIUtils.getPlaceHolder(ctx); 
      } else if 
(DrawerImageLoader.Tags.ACCOUNT_HEADER.name().equals(tag)) { 
       return new IconicsDrawable(ctx).iconText(" 
").backgroundColorRes(com.mikepenz.materialdrawer.R.color.primary) 
.sizeDp(56); 
      } else if ("customUrlItem".equals(tag)) { 
       return new IconicsDrawable(ctx).iconText(" 
").backgroundColorRes(R.color.md_red_500).sizeDp(56); 
      } 

      //we use the default one for 
      //DrawerImageLoader.Tags.PROFILE_DRAWER_ITEM.name() 

      return super.placeholder(ctx, tag); 
     } 
    }); 
} 
} 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="ir.techend.yusof.ghaza"> 

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 
</manifest> 
+0

你指的是什麼教程? – DroiDev

+0

mikepenz教程 – Yusof

+0

我應該知道這是什麼?你能提出一個參考嗎?你的問題..... nm - >你沒有 – DroiDev

回答

1

請刪除重複的

new DrawerBuilder().withActivity(this).build(); 

,你必須在你的MainActivity,你只能.build()一個Drawer(在某些情況下.append()另一個)

Drawer的代碼將確保不會2 DrawerLayout的充氣。

此外,我檢查標題圖像的網址。這個不存在。 (http://www.api.androidhive.info/images/nav-menu-header-bg.jpg返回404)

+0

嘿邁克,我有網絡問題,並沒有添加init映像加載程序。現在我很好。感謝你的圖書館;)。 – Yusof

0

使用畢加索庫爲

Picasso.with(YourActivityName.this).load(url_img).into(imageView); 

,並在您gradle這個依賴把

compile 'com.squareup.picasso:picasso:2.5.2'