【问题标题】:parsing json file as imageButton将 json 文件解析为 imageButton
【发布时间】:2016-09-01 02:45:41
【问题描述】:

我正在将 json 文件解析为 java for android app。

在我的 json 文件中有 imagebutton 的图片链接和点击播放的链接。 在应用程序中它显示了所有内容,但是当我单击图像按钮应用程序崩溃时。

{  "channels":
 [ 
    { 
        "name": "News",
        "link": "http://something.comchunks.m3u8", 
        "cat": "news", 
        "image": "http://www.logo.com/img.png" 
    } 
  ]

}

这里是Java代码:

public class ActorAdapter extends ArrayAdapter<Actors> {
    ArrayList<Actors> listItem;
    LayoutInflater inflater;
    int Resource;
    ViewHolder holder;
    private String url;


    public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
        super(context, resource, objects);
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        listItem = objects;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // convert view = design
        View v = convertView;

        if (v == null) {
            holder = new ViewHolder() ;
            v = inflater.inflate(Resource, null);
            holder.imageview = (ImageButton) v.findViewById(R.id.ivImage);
            holder.tvName = (TextView) v.findViewById(R.id.tvName);

            holder.tvcat = (TextView) v.findViewById(R.id.tvCountryt);
            holder.tvuri = (TextView) v.findViewById(R.id.link);


            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.imageview.setImageResource(R.drawable.ic_launcher);
        new DownloadImage(holder.imageview).execute(listItem.get(position).getImage());
        holder.tvName.setText(listItem.get(position).getName());

        holder.tvcat.setText(listItem.get(position).getcat());
        holder.tvuri.setText(listItem.get(position).getlink());
        return v;

    }

    static class ViewHolder {
        public ImageButton imageview;
        public TextView tvName;
        public TextView tvcat;
        public TextView tvuri;

    }

    private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

        ImageButton bmImage;

        public DownloadImage(ImageButton bmImage) {
            this.bmImage = bmImage;
        }

        @Override
        protected Bitmap doInBackground(String... urls) {
            // TODO Auto-generated method stub

            String urldisplay = urls[0];
             Bitmap Icon = null;

             try{
                  InputStream in = new java.net.URL(urldisplay).openStream();

                  Icon = BitmapFactory.decodeStream(in);

              }catch(Exception e){

                  Log.e("Error", e.getMessage());

                  e.printStackTrace();
              }

            return Icon;
        }

        /* (non-Javadoc)
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub

            bmImage.setImageBitmap(result);

        }

    }
}

记录堆栈跟踪

09-01 10:59:10.111 23927-23927/com.tutorial.Jsonparsing D/AndroidRuntime: Shutting down VM
09-01 10:59:10.111 23927-23927/com.tutorial.Jsonparsing W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x55d89b20)
09-01 10:59:10.111 23927-23927/com.tutorial.Jsonparsing I/Process: Sending signal. PID: 23927 SIG: 9
09-01 10:59:10.111 23927-23927/com.tutorial.Jsonparsing D/AndroidRuntime: procName from cmdline: com.tutorial.Jsonparsing
09-01 10:59:10.111 23927-23927/com.tutorial.Jsonparsing E/AndroidRuntime: in writeCrashedAppName, pkgName :com.tutorial.Jsonparsing
09-01 10:59:10.111 23927-23927/com.tutorial.Jsonparsing D/AndroidRuntime: file written successfully with content: com.tutorial.Jsonparsing StringBuffer : ;com.tutorial.Jsonparsing
09-01 10:59:10.111 23927-23927/com.tutorial.Jsonparsing E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.tutorial.Jsonparsing, PID: 23927
                                                                          java.lang.IllegalStateException: Could not find a method new(View) in the activity class com.android.tools.fd.runtime.BootstrapApplication for onClick handler on view class android.widget.ImageButton with id 'ivImage'
                                                                              at android.view.View$1.onClick(View.java:3815)
                                                                              at android.view.View.performClick(View.java:4443)
                                                                              at android.view.View$PerformClick.run(View.java:18442)
                                                                              at android.os.Handler.handleCallback(Handler.java:733)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:136)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:515)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                                              at dalvik.system.NativeStart.main(Native Method)
                                                                           Caused by: java.lang.NoSuchMethodException: new [class android.view.View]
                                                                              at java.lang.Class.getConstructorOrMethod(Class.java:472)
                                                                              at java.lang.Class.getMethod(Class.java:857)
                                                                              at android.view.View$1.onClick(View.java:3808)
                                                                              at android.view.View.performClick(View.java:4443) 
                                                                              at android.view.View$PerformClick.run(View.java:18442) 
                                                                              at android.os.Handler.handleCallback(Handler.java:733) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                              at android.os.Looper.loop(Looper.java:136) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 

【问题讨论】:

  • 请分享您的日志堆栈跟踪
  • 你的活动课是什么?

标签: java android json listview imagebutton


【解决方案1】:

@Fahad 问题可能在这一行

holder.imageview = (ImageButton) v.findViewById(R.id.ivImage);

您在持有人类中初始化了一个ImageView,现在您将其转换为ImageButton。将其替换为 ImageView 并确保您的布局+支架中的每个位置都有 ImageView 引用并正确投射。

【讨论】:

  • 不是问题
【解决方案2】:

你需要在ivimage..的地方添加布局

初始化...

private Context mContext;



 public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
    super(context, resource, objects);

 mContext=context
   }






 if (v == null) {
        holder = new ViewHolder() ;
        v = View.inflate(mContext, R.layout.row_layout, null);
        holder.imageview = (ImageButton) v.findViewById(R.id.ivImage);
       .....remaning code...

     }

【讨论】:

  • contxt here Youractivity.this
  • 我在 Fb 上给你发了消息,请检查
猜你喜欢
  • 2015-04-26
  • 2023-03-31
  • 1970-01-01
  • 2018-11-07
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多