0

Been on the same problem for too many hours, so I figured why not ask the stackoverflow community. I have implemented twitter it works fine if the orientation is in portrait. If I would turn the phone in landscape during the async call it crashes. I have seen a few examples of this issue and have tried a few of them. locking the screen is not a solution.
I'll try to post a snippet of the code. maybe someone can give me a tip. Thanks

Logcat Error

12-03 16:43:01.650  18351-18351/com.example.admin.football E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.admin.football.fragments.TwitterFragment.onTaskFinished(TwitterFragment.java:94)
            at com.example.admin.football.fragments.TwitterFragment$DownloadTwitterTask.onPostExecute(TwitterFragment.java:193)
            at com.example.admin.football.fragments.TwitterFragment$DownloadTwitterTask.onPostExecute(TwitterFragment.java:171)
            at android.os.AsyncTask.finish(AsyncTask.java:631)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4797)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

Java code:

 public TwitterFragment()
{
}
@Override
public void onTaskStarted() {
    isTaskRunning = true;
    load(listView);
}
// converts a string of JSON data into a Twitter object
private Twitter jsonToTwitter(String result) {
    Twitter twits = null;
    if (result != null && result.length() > 0) {
        try {
            Gson gson = new Gson();
            twits = gson.fromJson(result, Twitter.class);
        } catch (IllegalStateException ex) {
            // just eat the exception
        }
    }
    return twits;
}
@Override
public void onTaskFinished(String result) {
    if (spinner != null) {
        final Twitter twits = jsonToTwitter(result);
        if(twits!=null) {

            // lets write the results to the console as well
            for (Tweet tweet : twits) {
                Log.i(LOG_TAG, tweet.getText());
            }

            cardArrayAdapter = new CardArrayAdapter(getActivity().getApplicationContext(), R.layout.list_item_card);
            for (int i = 0; i < twits.size(); i++) {
                String dt = twits.get(i).getDateCreated();
                Card card = new Card(twits.get(i).getUser().getProfileImageUrl(), twits.get(i).getUser().getScreenName(), twits.get(i).toString(), dt.substring(0, 10));
                cardArrayAdapter.add(card);

            }
            listView.setAdapter(cardArrayAdapter);
            /*cardArrayAdapter.getFilter().filter(Hashtag, new Filter.FilterListener() {
                @Override
                public void onFilterComplete(int count) {
                    listView.setAdapter(cardArrayAdapter);
                }
            });*/
            spinner.setVisibility(View.GONE);
        }
        spinner.setVisibility(View.GONE);
    }
    isTaskRunning = false;
}
@Override
public void onDetach() {
    // All dialogs should be closed before leaving the activity in order to avoid
    // the: Activity has leaked window com.android.internal.policy... exception
    if (spinner != null) {
        spinner.setVisibility(View.GONE);
    }
    super.onDetach();
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // If we are returning here from a screen orientation
    // and the AsyncTask is still working, re-create and display the
    // progress dialog.
    if (isTaskRunning) {
        load(listView);
    }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{

    View rootView = inflater.inflate(R.layout.fragment_twitter, container, false);
    listView = (ListView) rootView.findViewById(R.id.card_listView);
    spinner = (ProgressBar)rootView.findViewById(R.id.progressBar1);
    spinner.setVisibility(View.GONE);
    listView.addHeaderView(new View(getActivity()));
    listView.addFooterView(new View(getActivity()));
    // call method to download tweets
    downloadTweets();
    return rootView;
}
public void load(View view){
    spinner.setVisibility(View.VISIBLE);
}
// download twitter timeline after first checking to see if there is a network connection
public void downloadTweets() {
    ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        load(listView);
        new DownloadTwitterTask(this).execute(ScreenName);
    } else {
        Log.v(LOG_TAG, "No network connection available.");
    }
}


// Uses an AsyncTask to download a Twitter user's timeline
//TODO cancel this when view is changed!
public class DownloadTwitterTask extends AsyncTask<String, Void, String> {
    private final TaskListener listener;
    final static String CONSUMER_KEY = "";
    final static String CONSUMER_SECRET = "";
    final static String TwitterTokenURL = "https://api.twitter.com/oauth2/token";
    final static String TwitterStreamURL = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=";
    public DownloadTwitterTask(TaskListener listener) {
        this.listener = listener;
    }
    @Override
    protected String doInBackground(String... screenNames) {
        String result = null;

        if (screenNames.length > 0) {
            result = getTwitterStream(screenNames[0]);
        }
        return result;
    }

    // onPostExecute convert the JSON results into a Twitter object (which is an Array list of tweets
    @Override
    protected void onPostExecute(String result) {
        listener.onTaskFinished(result);

    }

    // convert a JSON authentication object into an Authenticated object
    private Authenticated jsonToAuthenticated(String rawAuthorization) {
        Authenticated auth = null;
        if (rawAuthorization != null && rawAuthorization.length() > 0) {
            try {
                Gson gson = new Gson();
                auth = gson.fromJson(rawAuthorization, Authenticated.class);
            } catch (IllegalStateException ex) {
                // just eat the exception
            }
        }
        return auth;
    } 
Rami
  • 7,879
  • 12
  • 36
  • 66

2 Answers2

0

Add this line in your Activity entry in Manifest:

android:configChanges="layoutDirection|keyboardHidden|orientation|screenSize|screenLayout"
Rajat Mehra
  • 1,462
  • 14
  • 19
0

Well even if somebody gave a solution i want to try to give a better one. Using configChanges won't let your application to reload resources when the screen is rotated, this means that if you are using different layouts or other resources for landscapes and portaits they will be totally unuseful. You should call your AsyncTask from a fragment and use the setRetainInstance(true) method, here you will find a good explanation on how to do it.

MineConsulting SRL
  • 2,340
  • 2
  • 17
  • 32