Note that there are some explanatory texts on larger screens.

plurals
  1. POActivity closes when trying to asyncronously connect to twitter
    primarykey
    data
    text
    <p>I have been trying to program a twitter client for Android (using twitter4j). So far the idea is to have a simple GUI, and if there is not a file with the OAuth token in the SD Card, connect to the Twitter API using AsyncTask, get the URL for the authorization and open the default browser. However, the browser never runs. Depending on the different modifications I have made trying to fix this, either the Activity starts normally but the browser never starts or the Activity crashes. I have come to a point of a a little of frustation and confussion. Can someone point out what's wrong with my code?</p> <pre><code>public class StatusActivity extends Activity { private static final String TAG = "StatusActivity"; EditText editText; Button updateButton; File oauthfile = null; public Context context = getApplicationContext(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_status); Log.d(TAG, "started"); // Find views editText = (EditText) findViewById(R.id.editText); // updateButton = (Button) findViewById(R.id.buttonUpdate); oauthfile = new File("sdcard/auth_file.txt"); //Check if the file with the keys exist if (oauthfile.exists()==false){ Log.d(TAG, "file not created"); Context context = getApplicationContext(); Toast toast = Toast.makeText(context, "file not created.", Toast.LENGTH_SHORT); toast.show(); new Authorization(context).execute(); } } public void openBrowser (View v){ Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startActivity(browserIntent); Log.d(TAG, "onclick"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.status, menu); return true; } } class Authorization extends AsyncTask &lt;String, Integer, String&gt;{ String url = null; private Context context; Authorization(Context context) { this.context = context.getApplicationContext(); } public void onPreExecute() { super.onPreExecute(); Toast.makeText(context, "Invoke onPreExecute()", Toast.LENGTH_SHORT).show(); } @Override public String doInBackground(String... params) { ConfigurationBuilder configBuilder = new ConfigurationBuilder(); configBuilder.setDebugEnabled(true) //I have eliminated the keys from the question :) .setOAuthConsumerKey("XXXXXXXXXXXXXX") .setOAuthConsumerSecret("XXXXXXXXXXXXXXX"); Twitter OAuthTwitter = new TwitterFactory(configBuilder.build()).getInstance(); RequestToken requestToken = null; AccessToken accessToken = null; do{ try { requestToken = OAuthTwitter.getOAuthRequestToken(); url = requestToken.getAuthorizationURL(); } catch (TwitterException ex) { ex.printStackTrace(); } } while (accessToken==null); return url; } protected void onPostExecute(String result) { super.onPostExecute(result); Toast.makeText(context, "Opening browser.", Toast.LENGTH_SHORT).show(); Intent browserIntent = new Intent(Intent.ACTION_ALL_APPS, Uri.parse(url)); context.startActivity(browserIntent); } } </code></pre> <p>I know that at least checks if the file for the tokens exists because the toast "file not created" appears, and that the activity is able to run the browser if I press the button. The app has permissions to write in the SD card and use the Internet. Thanks in advance.</p> <p>Logcat Trace:</p> <pre><code>03-28 19:02:32.816: E/AndroidRuntime(278): FATAL EXCEPTION: main 03-28 19:02:32.816: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.versec.pardinus/com.versec.pardinus.StatusActivity}: java.lang.NullPointerException 03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 03-28 19:02:32.816: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99) 03-28 19:02:32.816: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123) 03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627) 03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method) 03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521) 03-28 19:02:32.816: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 03-28 19:02:32.816: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 03-28 19:02:32.816: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method) 03-28 19:02:32.816: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException 03-28 19:02:32.816: E/AndroidRuntime(278): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100) 03-28 19:02:32.816: E/AndroidRuntime(278): at com.versec.pardinus.StatusActivity.&lt;init&gt;(StatusActivity.java:30) 03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.Class.newInstanceImpl(Native Method) 03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.Class.newInstance(Class.java:1429) 03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 03-28 19:02:32.816: E/AndroidRuntime(278): ... 11 more </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload