我正在开发一个应用程序,我必须使用picasso的库从url加载图像。 当我运行我的应用程序,它崩溃,我不知道这是什么问题。 有人可以帮我吗?
这是我的主要活动代码:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.GridView;
public class MainActivityLazy extends ActionBarActivity
{
String[] urls;
GridView gridview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_lazy);
urls = new String[] { "http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Sad-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Happy-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Crying-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Cool-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-In-love-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Lol-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Surprised-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Tongue-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Wink-icon.png",
"http://androidexample.com/media/webservice/LazyListView_images/image0.png",
"http://androidexample.com/media/webservice/LazyListView_images/image1.png",
"http://androidexample.com/media/webservice/LazyListView_images/image2.png",
"http://androidexample.com/media/webservice/LazyListView_images/image3.png",
"http://androidexample.com/media/webservice/LazyListView_images/image4.png",
"http://androidexample.com/media/webservice/LazyListView_images/image5.png",
"http://androidexample.com/media/webservice/LazyListView_images/image6.png",
"http://androidexample.com/media/webservice/LazyListView_images/image7.png",
"http://androidexample.com/media/webservice/LazyListView_images/image8.png",
"http://androidexample.com/media/webservice/LazyListView_images/image9.png",
"http://androidexample.com/media/webservice/LazyListView_images/image10.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Sad-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Happy-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Crying-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Cool-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-In-love-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Lol-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Surprised-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Tongue-icon.png",
"http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Emoticons-Wink-icon.png"
};
GridViewAdapter adapter = new GridViewAdapter(MainActivityLazy.this, urls);
gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的gridviewadapter代码:
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.squareup.picasso.Picasso;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class GridViewAdapter extends BaseAdapter
{
private String[] mUrls;
private Context mContext;
Bitmap bitmap;
private static LayoutInflater inflater=null;
public GridViewAdapter(Context context, String[] urls) {
//super(context, flag);
mContext = context;
mUrls = urls;
}
@Override
public int getCount() {
return mUrls.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_row, null);
}
ImageView imageView = (ImageView)convertView.findViewById(R.id.image);
Picasso.with(mContext).load(mUrls[position]).into(imageView);
return convertView;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public Bitmap getBitmapFromURL(String src)
{
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
这是我的logcat错误:
07-30 09:54:58.995: E/MoreInfoHPW_ViewGroup(23926): Parent view is not a TextView
07-30 09:54:59.070: E/AndroidRuntime(23926): FATAL EXCEPTION: main
07-30 09:54:59.070: E/AndroidRuntime(23926): Process: com.example.lazyload, PID: 23926
07-30 09:54:59.070: E/AndroidRuntime(23926): java.lang.IllegalArgumentException: Target must not be null.
07-30 09:54:59.070: E/AndroidRuntime(23926): at com.squareup.picasso.RequestCreator.into(RequestCreator.java:479)
07-30 09:54:59.070: E/AndroidRuntime(23926): at com.squareup.picasso.RequestCreator.into(RequestCreator.java:462)
07-30 09:54:59.070: E/AndroidRuntime(23926): at com.example.lazyload.GridViewAdapter.getView(GridViewAdapter.java:45)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.widget.AbsListView.obtainView(AbsListView.java:2733)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.widget.GridView.onMeasure(GridView.java:1049)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.View.measure(View.java:17357)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.View.measure(View.java:17357)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.View.measure(View.java:17357)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
07-30 09:54:59.070: E/AndroidRuntime(23926): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:382)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.View.measure(View.java:17357)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-30 09:54:59.070: E/AndroidRuntime(23926): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.View.measure(View.java:17357)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2175)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1316)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1513)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6388)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.Choreographer.doCallbacks(Choreographer.java:603)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.Choreographer.doFrame(Choreographer.java:573)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.os.Handler.handleCallback(Handler.java:733)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.os.Handler.dispatchMessage(Handler.java:95)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.os.Looper.loop(Looper.java:157)
07-30 09:54:59.070: E/AndroidRuntime(23926): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-30 09:54:59.070: E/AndroidRuntime(23926): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 09:54:59.070: E/AndroidRuntime(23926): at java.lang.reflect.Method.invoke(Method.java:515)
07-30 09:54:59.070: E/AndroidRuntime(23926): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-30 09:54:59.070: E/AndroidRuntime(23926): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-30 09:54:59.070: E/AndroidRuntime(23926): at dalvik.system.NativeStart.main(Native Method)
感谢。
答案 0 :(得分:1)
这些是您的日志猫的关键行。
07-30 09:54:58.995: E/MoreInfoHPW_ViewGroup(23926): Parent view is not a TextView
07-30 09:54:59.070: E/AndroidRuntime(23926): java.lang.IllegalArgumentException: Target must not be null.
07-30 09:54:59.070: E/AndroidRuntime(23926): at com.example.lazyload.GridViewAdapter.getView(GridViewAdapter.java:45)
这一行是异常的原因: Picasso.with(mContext).load(mUrls [位置])代入(ImageView的);
在GridViewAdapter.getView()
的第45行,您遇到了IllegalArgumentException。使用Log.d检查您发送到毕加索的内容。关于第一条错误消息,我猜测R.id.image
是TextView
而不是ImageView
。