我正在开发通过API获取数据的电影Android应用程序,有两种方式按人气或最高费率显示电影我使用设置从这两种方式中进行选择,但是当我按设置时应用程序崩溃。
这是MainActivity
package com.example.ahmed.moviesapp;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.Toast;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String Target_URL = "";
ArrayList <Movie> MoviesList ;
GridView movieGrid ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showUserSettings();
movieGrid = (GridView)findViewById(R.id.gridView);
if (savedInstanceState != null) {
MoviesList = (ArrayList<Movie>)
savedInstanceState.getSerializable("movies");
afterAsync();
}
else {
Task mytask = new Task(this);
mytask.execute("1","2","3");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main
, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent i = new Intent(this, UserSettingActivity.class);
startActivityForResult(i, 1);
break;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
showUserSettings();
break;
}
}
private void showUserSettings() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
Target_URL =
sharedPrefs.getString("sortby","http://api.themoviedb.org/3/movie/top_rated?
api_key=###################");
}
private void afterAsync (){
MovieAdapter adapter = new MovieAdapter(MainActivity.this ,
R.layout.single_item, MoviesList);
adapter.printfun();
movieGrid.setAdapter(adapter);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("movies", MoviesList);
}
private class Task extends AsyncTask<String,String,String>{
public ProgressDialog dialog;
Context context;
ServerConnection connection = new ServerConnection();
public Task(Context context) {
this.context = context;
dialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Loading...");
dialog.show();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (dialog != null && dialog.isShowing())
dialog.dismiss();
afterAsync ();
}
@Override
protected String doInBackground(String... params) {
String jsonToParse = connection.getDatat(Target_URL);
Log.e("Error",Target_URL);
MoviesList = JsonParser.MoviesParser(jsonToParse);
return null;
}
}
}
UserSettingActivity
public class UserSettingActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_settings"
ic_menu_preferences=""
android:orderInCategory="100"
android:showAsAction="never"
android:title="Settings"
android:icon="@android:drawable/ic_menu_preferences"/>
</menu>
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sortby">
<item>View By Top Rated</item>
<item>View By Highest Popularity</item>
</string-array>
<string-array name="sortbyvalues">
<item>1</item>
<item>2</item>
</string-array>
的strings.xml
<resources>
<string name="app_name">MoviesApp</string>
<string name="title">View By</string>
<string name="sortbysummary">Set the method you prefer</string>
<string name="popular">http://api.themoviedb.org/3/movie/popular?
api_key=######</string>
<string name="top_rated">http://api.themoviedb.org/3/movie/top_rated?
api_key=#######</string>
</resources>
的settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
>
<PreferenceCategory android:title="Settings" >
<ListPreference
android:key="sortbyKey"
android:entries="@array/sortby"
android:summary="@string/sortbysummary"
android:entryValues="@array/sortbyvalues"
android:title="@string/title" />
</PreferenceCategory>
</PreferenceScreen>
logcat的
08-26 23:36:49.369 31552-31552/com.example.ahmed.moviesapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Parcel: unable to marshal value com.example.ahmed.moviesapp.Movie@4064c140
at android.os.Parcel.writeValue(Parcel.java:1132)
at android.os.Parcel.writeList(Parcel.java:519)
at android.os.Parcel.writeValue(Parcel.java:1092)
at android.os.Parcel.writeMapInternal(Parcel.java:488)
at android.os.Bundle.writeToParcel(Bundle.java:1552)
at android.os.Parcel.writeBundle(Parcel.java:502)
at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1622)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2298)
at android.app.ActivityThread.access$1700(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)