为什么我的应用程序“不幸在手机停止”时它在模拟器中工作?

时间:2016-03-03 18:54:26

标签: android android-intent memory-leaks buffer android-logcat

请帮助我,我正在尝试制作电影信息应用程序,从热门电影的节目海报开始,它在模拟器中工作但不能在手机中工作并显示“不幸的应用已经停止”,经过很长一段时间我发现了一些logcat中的错误,我不明白。那么如何解决它以及我实施错误的地方。 Logcat错误

03-03 23:43:47.764 8548-8548/? E/memtrack: Couldn't load memtrack module (No such file or directory)
03-03 23:43:47.764 8548-8548/? E/android.os.Debug: failed to load memtrack module: -2
03-03 23:43:49.460 8557-8557/? E/memtrack: Couldn't load memtrack module (No such file or directory)
03-03 23:43:49.461 8557-8557/? E/android.os.Debug: failed to load memtrack module: -2
03-03 23:43:49.471 8561-8561/? E/memtrack: Couldn't load memtrack module (No such file or directory)
03-03 23:43:49.471 8561-8561/? E/android.os.Debug: failed to load memtrack module: -2
03-03 23:43:49.563 941-941/? E/EGL_emulation: tid 941: eglCreateSyncKHR(1294): error 0x3004 (EGL_BAD_ATTRIBUTE)
03-03 23:43:50.279 8402-8415/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7d3ee0
03-03 23:43:50.610 941-1031/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container, new ForecastFragment(), ForecastFragment.class.getSimpleName())
                .commit();

    }
public class ForecastFragment extends Fragment {
    //Constructor
    public ForecastFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        String Popular_query = "http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=XXXXXXXXXXXXXXXX";

         new ForecastTask().execute(Popular_query);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    //GetJSONData online-----------------------------
    public class ForecastTask extends AsyncTask<String, String, List<MovieModel>> {

        private final String LOG_TAG= ForecastTask.class.getSimpleName();
        @Override
        protected List<MovieModel> doInBackground(String... params) {
            if (params.length==0){
                return null;
            }
            HttpURLConnection connection=null;
            BufferedReader reader=null;

            try {
                URL url=new URL(params[0]);
                connection=(HttpURLConnection)url.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();

                InputStream inputStream=connection.getInputStream();
                if (inputStream==null){
                    return null;
                }

                //Create String Buffer object
                StringBuffer buffer=new StringBuffer();
                reader=new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while((line=reader.readLine()) !=null){
                    buffer.append(line);
                }

                JSONObject jsonObject=new JSONObject(buffer.toString());
                JSONArray jsonArray= jsonObject.getJSONArray("results");

                //create Array list form moviemodel class
                List<MovieModel> movieModelList=new ArrayList<>();

                for(int i= 0; i<jsonArray.length();i++){
                    JSONObject finalObject=jsonArray.getJSONObject(i);

                    MovieModel movieModel=new MovieModel();
                    movieModel.setId(finalObject.getInt("id"));
                    movieModel.setPoster_path(finalObject.getString("poster_path"));
                    movieModel.setTitle(finalObject.getString("title"));
                    movieModel.setRelease_date(finalObject.getString("release_date"));
                    movieModel.setVote_average((float) finalObject.getDouble("vote_average"));
                    movieModel.setOverview(finalObject.getString("overview"));
                    movieModel.setAdult(finalObject.getBoolean("adult"));
                    movieModel.setBackdrop_path(finalObject.getString("backdrop_path"));

                // adding object into list
                    movieModelList.add(movieModel);
                }
                return movieModelList;

            } catch (JSONException | IOException e) {
                e.printStackTrace();
            } finally {
                if (connection!=null){
                    connection.disconnect();
                }
                try {
                    if(reader!=null){
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<MovieModel> result) {
            super.onPostExecute(result);
            if(result!=null){
                //calling MovieAdapter class by object with passing 3 parameters
                MovieAdapter movieAdapter=new MovieAdapter(getContext()
                        , R.layout.image_list,
                        result);
                GridView gridView=(GridView)getActivity().findViewById(R.id.gridview);
                gridView.setAdapter(movieAdapter);
            }
        }
    }
}


public class MovieAdapter extends ArrayAdapter {
    private List<MovieModel> movieList;
    private int resource;
    private LayoutInflater inflater;
    public MovieAdapter(Context context, int resource, List<MovieModel> objects) {
        super(context, resource, objects);
        movieList=objects;
        this.resource=resource;
        inflater=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null) {
            convertView = inflater.inflate(R.layout.image_list, parent,false);
        }
        String data= movieList.get(position)
                .getPoster_path();

        ImageView imageView = (ImageView)convertView.findViewById(R.id.imageview);
        Picasso.with(convertView.getContext())
                .load("http://image.tmdb.org/t/p/w185/"+data)
                .resize(550, 700)
                .into(imageView);
        return convertView;
    }
}

public class MovieModel {

    private int id;
    private String poster_path;
    private String title;
    private float vote_average;
    private String overview;
    private String release_date;
    private String backdrop_path;
    private Boolean adult;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPoster_path() {
        return poster_path;
    }

    public void setPoster_path(String poster_path) {
        this.poster_path = poster_path;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public float getVote_average() {
        return vote_average;
    }

    public void setVote_average(float vote_average) {
        this.vote_average = vote_average;
    }

    public String getOverview() {
        return overview;
    }

    public void setOverview(String overview) {
        this.overview = overview;
    }

    public String getRelease_date() {
        return release_date;
    }

    public void setRelease_date(String release_date) {
        this.release_date = release_date;
    }

    public String getBackdrop_path() {
        return backdrop_path;
    }

    public void setBackdrop_path(String backdrop_path) {
        this.backdrop_path = backdrop_path;
    }

    public Boolean getAdult() {
        return adult;
    }

    public void setAdult(Boolean adult) {
        this.adult = adult;
    }

}

XML布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/fragment_container"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.santossingh.popularmoviesapp.MainActivity"
    tools:showIn="@layout/activity_main">

            <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="auto_fit"
            >
            </GridView>

</FrameLayout>

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/imageview"
    >

</ImageView>

0 个答案:

没有答案