我正在嘗試使用Picasso將圖像加載到ImageView。最基本的東西。畢加索將無法加載圖像(Android)
我知道這很簡單,我很確定我做得很好,但是由於某種原因它不起作用。沒有照片顯示在我的應用程序。我發現有人遇到同樣的問題(例如,Android - Picasso in Android Studio doesn't load),我的代碼與他們的固定版本幾乎完全相同,但它仍然無效。
這是我的代碼。
在MainActivity.java,我有
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new MyFragment())
.commit();
}
而在MyFragment.java,我有
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageView image = (ImageView) rootView.findViewById(R.id.movie_image);
Picasso.with(getActivity()).load(Uri.parse("http://i.imgur.com/DvpvklR.png")).into(image);
return rootView;
}
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/movie_image" />
</FrameLayout>
清單文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.popmovies">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<user-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
的build.gradle(模塊:APP)
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.admin.popmovies"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'
}
repositories {
mavenCentral();
}
任何建議,將不勝感激!
編輯:logcat的 我得到了一些消息在logcat中畢加索以前,但我檢查了一遍,現在還沒有從畢加索日誌。 它看起來酷似有看起來像這樣同樣的問題,其他線程..
02-23 16:41:36.857 28865-28865/com.example.enrico.prova D/Picasso﹕ Main created [R1] Request{http://www.online-image-editor.com//styles/2014/images/example_image.png}
02-23 16:41:36.858 28865-28884/com.example.enrico.prova D/Picasso﹕ Dispatcher enqueued [R1]+0ms
02-23 16:41:36.876 28865-29238/com.example.enrico.prova D/Picasso﹕ Hunter executing [R1]+16ms
02-23 16:41:36.883 28865-28884/com.example.enrico.prova D/Picasso﹕ Dispatcher batched [R1]+25ms for error
02-23 16:41:37.112 28865-28884/com.example.enrico.prova D/Picasso﹕ Dispatcher delivered [R1]+254ms
02-23 16:41:37.112 28865-28865/com.example.enrico.prova D/Picasso﹕ Main errored [R1]+255ms
UPDATE:
我想通了!這是因爲我的應用中的目標SDK版本是23,而我的手機使用的是KitKat。當我將gradle中的依賴項改爲compile 'com.android.support:appcompat-v7:21.0.2'
而不是compile 'com.android.support:appcompat-v7:23.1.1'
時,它起作用。
再次感謝您的所有建議!
load(「http://i.imgur.com/DvpvklR.png」)使用此 – Madhur