尝试列出customView适配器

时间:2015-11-27 16:55:23

标签: android json android-studio android-custom-view custom-adapter

我试图从imdb中获取数据并显示与其相邻的标题和年份的相关图片。已设法显示标题和年份,但在尝试完全显示时,应用程序在搜索时崩溃。

这是我的成绩课

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

public class ResultsActivity extends AppCompatActivity {

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

    String keyword = getIntent().getExtras().getString("movieTitle");
    keyword = keyword.replaceAll("\\s", "+");

    FilmDatabase db = new FilmDatabase(this);


    String jsonResult = db.getResult(keyword);

    if (jsonResult == null){
        jsonResult = runSearch(keyword);
        db.insertData(keyword, jsonResult);
    }
    ArrayList<Film> searchResults = getValuesFromJSON(jsonResult);

    ListView listView = (ListView) findViewById(R.id.listView);





    CustomAdapter customAdapter = new CustomAdapter(ResultsActivity.this, searchResults);
    listView.setAdapter(customAdapter);

    final Button Back = (Button) findViewById(R.id.Back);

    Back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(ResultsActivity.this, SearchActivity.class);
            startActivity(i);
        }
    });

    }





private String runSearch(String keyword){
    String jsonResult = null;


    try {
        URL url = new URL("https://www.omdbapi.com/?s=" + keyword);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        InputStream input = httpURLConnection.getInputStream();
        Scanner scanner = new Scanner(input, "UTF-8").useDelimiter("\\A");
        jsonResult = scanner.hasNext() ? scanner.next() : "";
    } catch (Exception e){
        e.printStackTrace();
    }

    return jsonResult;
}


private ArrayList<Film> getValuesFromJSON(String jsonResult){

    ArrayList<Film> results = new ArrayList<Film>();

    try {
        JSONObject jsonObject = new JSONObject(jsonResult);
        JSONArray jsonArray = jsonObject.getJSONArray("Search");
        for (int i=0; i<jsonArray.length(); i++){
            JSONObject result = jsonArray.getJSONObject(i);
            Film film = new Film(result);
            results = film.fromJson(jsonArray);

        }

    } catch (Exception e){
        e.printStackTrace();
    }


    return results;
}
}`

这是我的自定义适配器类

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.util.ArrayList;


class CustomAdapter extends ArrayAdapter<Film> {

public CustomAdapter(Context context, ArrayList<Film> results) {
    super(context,0, results);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    super.getView(position, convertView, parent);
    LayoutInflater filmInflater = LayoutInflater.from(getContext());
    View customView = filmInflater.inflate(R.layout.custom_row, parent, false);

    Film film = getItem(position);
    TextView textView = (TextView) customView.findViewById(R.id.textView);
    ImageView imageView = (ImageView) customView.findViewById(R.id.imageView);

    textView.setText(film.title + " - Year: " + film.year);
    new DownloadImageTask((ImageView) imageView).execute(film.poster);


    return customView;

}




private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}
}

这是我的电影数据库课程

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class Film {
public String title;
public String year;
public String poster;


public Film(JSONObject object){
    try{
        this.title = object.getString("Title");
        this.year = object.getString("Year");
        this.poster = object.getString("Poster");
    }catch(JSONException e){
        e.printStackTrace();
    }

}

public static ArrayList<Film> fromJson(JSONArray jsonArray){
    ArrayList<Film> films = new ArrayList<Film>();
    for (int i = 0; i < jsonArray.length(); i++){
        try{
            films.add(new Film(jsonArray.getJSONObject(i)));
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
    return films;
}
}

这是我的custom_row xml布局文件          

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView" />
</LinearLayout>

完整崩溃日志     11-28 18:17:58.607 1555-1555 /? I / LatinIME:开始输入。光标位置= 0,0 11-28 18:17:58.612 951-1016 /? E / SurfaceFlinger:ro.sf.lcd_density必须定义为构建属性 11-28 18:17:58.612 1555-2512 /? D / OpenGLRenderer:使用EGL_SWAP_BEHAVIOR_PRESERVED:true 11-28 18:17:58.621 1555-1555 /? D /:HostConnection :: get()建立新的主机连接0xad71cd20,tid 1555 11-28 18:17:58.657 1555-2512 /? D /:HostConnection :: get()建立新主机连接0xad71ce00,tid 2512 11-28 18:17:58.671 1555-2512 /? I / OpenGLRenderer:初始化的EGL,版本1.4 11-28 18:17:58.723 1555-2512 /? W / EGL_emulation:eglSurfaceAttrib未实现 11-28 18:17:58.723 1555-2512 /? W / OpenGLRenderer:无法在表面0xad6faa80上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS 11-28 18:18:02.244 1299-1623 /? I / ActivityManager:来自显示0的uid 10058的START u0 {cmp = uk.ac.city.acnh550.searchitunes / .ResultsActivity(has extras)} 11-28 18:18:02.326 2494-2494 /? W / ResourceType:获取资源号0x00000000的值时没有包标识符 11-28 18:18:02.326 2494-2494 /? D / AndroidRuntime:关闭VM 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:致命异常:主要 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:流程:uk.ac.city.acnh550.searchitunes,PID:2494 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:android.content.res.Resources $ NotFoundException:资源ID#0x0 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.content.res.Resources.getValue(Resources.java:1351) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.content.res.Resources.loadXmlResourceParser(Resources.java:2774) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.content.res.Resources.getLayout(Resources.java:1165) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.LayoutInflater.inflate(LayoutInflater.java:421) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:378) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.ArrayAdapter.getView(ArrayAdapter.java:369) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:at uk.ac.city.acnh550.searchitunes.CustomAdapter.getView(CustomAdapter.java:30) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.AbsListView.obtainView(AbsListView.java:2346) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.ListView.measureHeightOfChildren(ListView.java:1280) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.ListView.onMeasure(ListView.java:1188) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.View.measure(View.java:18788) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:715) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.View.measure(View.java:18788) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.View.measure(View.java:18788) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.View.measure(View.java:18788) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.View.measure(View.java:18788) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.View.measure(View.java:18788) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:at com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2643) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.View.measure(View.java:18788) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.Choreographer.doCallbacks(Choreographer.java:670) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.Choreographer.doFrame(Choreographer.java:606) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.os.Handler.handleCallback(Handler.java:739) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:95) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.os.Looper.loop(Looper.java:148) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:5417) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:at java.lang.reflect.Method.invoke(Native Method) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 11-28 18:18:02.326 2494-2494 /? E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 11-28 18:18:02.334 1299-1636 /? W / ActivityManager:强制完成活动uk.ac.city.acnh550.searchitunes / .ResultsActivity 11-28 18:18:02.344 1299-1636 /? W / ActivityManager:强制完成活动uk.ac.city.acnh550.searchitunes / .SearchActivity 11-28 18:18:02.418 1299-1807 /? I / OpenGLRenderer:初始化的EGL,版本1.4 11-28 18:18:02.453 1299-1807 /? W / EGL_emulation:eglSurfaceAttrib未实现 11-28 18:18:02.453 1299-1807 /? W / OpenGLRenderer:无法在表面0x9c9dabc0上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS 11-28 18:18:02.860 1299-1313 /? W / ActivityManager:ActivityRecord的活动暂停超时{733fd5 u0 uk.ac.city.acnh550.searchitunes / .ResultsActivity t31 f} 11-28 18:18:03.363 1299-1314 /? I / Choreographer:跳过54帧!应用程序可能在其主线程上做了太多工作。 11-28 18:18:03.505 2419-2437 /? W / EGL_emulation:eglSurfaceAttrib未实现 11-28 18:18:03.505 2419-2437 /? W / OpenGLRenderer:无法在表面0xa27d99a0上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS 11-28 18:18:04.752 2494-2494 /? I /处理:发送信号。 PID:2494 SIG:9 11-28 18:18:04.756 1299-1329 /? W / InputDispatcher:channel&#39; f2bf72e uk.ac.city.acnh550.searchitunes / uk.ac.city.acnh550.searchitunes.SearchActivity(server)&#39; 〜消费者关闭输入通道或发生错误。事件= 0x9 11-28 18:18:04.756 1299-1329 /? E / InputDispatcher:channel&#39; f2bf72e uk.ac.city.acnh550.searchitunes / uk.ac.city.acnh550.searchitunes.SearchActivity(server)&#39; 〜频道不可挽回地被打破,将被处置! 11-28 18:18:04.756 1299-1329 /? W / InputDispatcher:channel&#39; fb50451 uk.ac.city.acnh550.searchitunes / uk.ac.city.acnh550.searchitunes.ResultsActivity(server)&#39; 〜消费者关闭输入通道或发生错误。事件= 0x9 11-28 18:18:04.756 1299-1329 /? E / InputDispatcher:channel&#39; fb50451 uk.ac.city.acnh550.searchitunes / uk.ac.city.acnh550.searchitunes.ResultsActivity(server)&#39; 〜频道不可挽回地被打破,将被处置! 11-28 18:18:04.757 1299-1310 /? I / WindowState:WIN DEATH:Window {fb50451 u0 uk.ac.city.acnh550.searchitunes / uk.ac.city.acnh550.searchitunes.ResultsActivity}

1 个答案:

答案 0 :(得分:0)

我认为存在语法问题,因此无法生成R文件。 仔细检查custom_row文件。

P.S如果您使用的是Android Studio它会帮助您解决语法错误的问题。 (如果您已经使用,则使缓存无效并重新启动)