我想从asynctask使用UIL。
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class SingleAppDetail extends Activity {
Button btnLogout;
Button btShowProgress;
// Progress Dialog
private ProgressDialog pDialog;
JSONparser jParser = new JSONparser();
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0;
private static String file_url = "http://site.com/as.apk";
JSONArray detalii = null;
TextView app_Detail_title;
Button app_price_button;
TextView app_Description;
ImageView app_Picture;
View loadingPanelViev;
View appDetailHeader;
RatingBar appDetailRating;
RatingBar rating_1;
RatingBar rating_2;
RatingBar rating_3;
RatingBar rating_4;
RatingBar rating_5;
RatingBar rating_ff;
TextView rating_ff_text;
TextView rating_1_text;
TextView rating_2_text;
TextView rating_3_text;
TextView rating_4_text;
TextView rating_5_text;
String[] imageUrls;
ImageSwitcher appGallery;
DisplayImageOptions options;
com.nostra13.universalimageloader.core.ImageLoader ImageLoader;
Gallery app_gallery;
ArrayList<String> urlse = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.app_detail);
app_Detail_title = (TextView) findViewById(R.id.app_detail_title);
app_price_button = (Button) findViewById(R.id.app_detail_price);
app_Description = (TextView) findViewById(R.id.description_holder);
app_Picture = (ImageView) findViewById(R.id.singleAppDetailImage);
loadingPanelViev = (View) findViewById(R.id.loadingPanelAppDetailHeader);
appDetailHeader = (View) findViewById(R.id.app_detail_header_content);
appDetailRating = (RatingBar) findViewById(R.id.app_detail_rating);
rating_1 = (RatingBar) findViewById(R.id.rating_1);
rating_2 = (RatingBar) findViewById(R.id.rating_2);
rating_3 = (RatingBar) findViewById(R.id.rating_3);
rating_4 = (RatingBar) findViewById(R.id.rating_4);
rating_5 = (RatingBar) findViewById(R.id.rating_5);
rating_ff = (RatingBar) findViewById(R.id.rating_ff);
rating_ff_text = (TextView) findViewById(R.id.rating_ff_text);
rating_1_text = (TextView) findViewById(R.id.rating_1_text);
rating_2_text = (TextView) findViewById(R.id.rating_2_text);
rating_3_text = (TextView) findViewById(R.id.rating_3_text);
rating_4_text = (TextView) findViewById(R.id.rating_4_text);
rating_5_text = (TextView) findViewById(R.id.rating_5_text);
// appGallery=(ImageSwitcher)findViewById(R.id.app_detail_gallery);
app_gallery = (Gallery) findViewById(R.id.app_gallery);
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory().build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext()).defaultDisplayImageOptions(
defaultOptions).build();
ImageLoader.getInstance().init(config);
new loadAppMainDetails(getApplicationContext()).execute();
new loadAppMainRating(getApplicationContext()).execute();
new loadAppMainImages(getApplicationContext()).execute();
Button download = (Button) findViewById(R.id.app_detail_price);
download.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DownloadFileFromURL().execute(file_url);
}
});
}
/**
* Showing Dialog
* */
@Override
protected Dialog onCreateDialog(int id) {
//this is ok
}
/**
* Background Async Task to download file
* */
class DownloadFileFromURL extends AsyncTask<String, String, String> {
//this is ok
}
class loadAppMainDetails extends
AsyncTask<String, String, HashMap<String, String>> {
//this is ok
}
class loadAppMainRating extends AsyncTask<String, String, String> {
this is ok;
}
// here starts the problem
class loadAppMainImages extends AsyncTask<String, String, String> {
Context mContext;
JSONArray appDetailPictures;
String image_path;
public loadAppMainImages(Context context) {
super();
this.mContext = context;
}
@Override
protected String doInBackground(String... params) {
Intent is = getIntent();
String app_id = is.getStringExtra("element_id");
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("app_id", app_id));
JSONObject json = jParser.makeHttpRequest(
"http://himbimarket.com/masterApp/get_app_pictures.php",
"POST", parameters);
try {
int success = json.getInt("success");
if (success == 1) {
appDetailPictures = json.getJSONArray("images");
for (int i = 0; i < appDetailPictures.length(); i++) {
JSONObject c = appDetailPictures.getJSONObject(i);
// Storing each json item in variable
String picname = c.getString("picture");
image_path = "http://www.himbimarket.com/masterApp/"
.concat(picname);
urlse.add(image_path);
}
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), urlse.get(2),
Toast.LENGTH_LONG).show();
app_gallery.setAdapter(new ImageGalleryAdapter());
}
}
public class ImageGalleryAdapter extends BaseAdapter {
@Override
public int getCount() {
return urlse.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = (ImageView) convertView;
if (imageView == null) {
imageView = (ImageView) getLayoutInflater().inflate(
R.layout.item_gallery_image, parent, false);
}
ImageLoader.displayImage(urlse.get(position), imageView);
return imageView;
}
}
}
我不知道为什么不起作用。
错误代码如下:
04-19 06:52:19.635: E/AndroidRuntime(1593): FATAL EXCEPTION: main
04-19 06:52:19.635: E/AndroidRuntime(1593): java.lang.NullPointerException
04-19 06:52:19.635: E/AndroidRuntime(1593): at com.himbi.market.SingleAppDetail$ImageGalleryAdapter.getView(SingleAppDetail.java:471)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.View.measure(View.java:15513)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.View.measure(View.java:15513)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.View.measure(View.java:15513)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.ScrollView.onMeasure(ScrollView.java:321)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.View.measure(View.java:15513)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.View.measure(View.java:15513)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.View.measure(View.java:15513)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-19 06:52:19.635: E/AndroidRuntime(1593): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.View.measure(View.java:15513)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.Choreographer.doFrame(Choreographer.java:532)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-19 06:52:19.635: E/AndroidRuntime(1593): at android.os.Handler.handleCallback(Handler.java:725)
当他尝试加载imageview时,它在适配器中遇到了问题。
这是app_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/app_detail_back_image" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/app_detail_back_image"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/loadingPanelAppDetailHeader"
style="@style/GenericProgressBackground"
android:layout_height="fill_parent" >
<ProgressBar style="@style/GenericProgressIndicator" />
</RelativeLayout>
<LinearLayout
android:id="@+id/app_detail_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/divider"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="@+id/singleAppDetailImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/nopic" />
<RelativeLayout
android:id="@+id/app_detail_header_content"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<includes.TextResize
android:id="@+id/app_detail_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:maxLines="2"
android:text="First Application"
android:textColor="@color/himbi_apps_color"
android:textSize="22sp" />
<Button
android:id="@+id/app_detail_price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/app_detail_price_back"
android:text="Button" />
<TextView
android:id="@+id/app_detail_developer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/app_detail_title"
android:text="TextView"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp" >
<Gallery
android:id="@+id/app_gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:spacing="1dip" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<RatingBar
android:id="@+id/app_detail_rating"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/app_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Small Tijkjext"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/description_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Description Holder" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/rating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/himbi_color" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="25dp"
android:layout_toRightOf="@+id/relativeLayout2" >
<TextView
android:id="@+id/rating_ff_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView8"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:text="4.5"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/himbi_apps_color" />
<RatingBar
android:id="@+id/rating_ff"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rating_ff_text"
android:layout_centerHorizontal="true"
android:padding="2dp" />
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#a0a0a0"
android:gravity="center_horizontal"
android:text="Rating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/header" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
用于膨胀的xml:
<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:spacing="1dip" />
Android版本为4.0.1,UIL是最新版本。
谢谢
答案 0 :(得分:5)
1)确保在Application类中初始化ImageLoader实例:
public class MyApplication extends Application {
@Override
public void onCreate() {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).build();
ImageLoader.getInstance().init(config);
}
}
2)另外,在Manifest中定义这个类:
< application
android:name=".MyApplication">
......
/>
3)另外,在使用之前初始化imageLoader:
imageLoader = ImageLoader.getInstance();
4)最后,使用它:
imageLoader.displayImage(urlse.get(position), imageView);
答案 1 :(得分:1)
ImageLoader.getInstance()。DisplayImage ...