0

I'm trying to open up a browser from my app.

Heres my code:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browserIntent); 

But I keep on getting this error:

04-08 18:07:26.117 6133-6133/com.example.android.quakereport E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.quakereport, PID: 6133 java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference at android.app.Activity.startActivityForResult(Activity.java:4473) at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:65) at android.app.Activity.startActivityForResult(Activity.java:4430) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:711) at android.app.Activity.startActivity(Activity.java:4791) at android.app.Activity.startActivity(Activity.java:4759) at com.example.android.quakereport.EarthquakeActivity.startIntent(EarthquakeActivity.java:53) at com.example.android.quakereport.EarthquakeAdapter$1.onClick(EarthquakeAdapter.java:68) at android.view.View.performClick(View.java:6256) at android.view.View$PerformClick.run(View.java:24701) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Here is my EarthquakeAdapter code:

currentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String url = QueryUtils.getURL(positionNum); EarthquakeActivity ea = new EarthquakeActivity(); ea.startIntent(url); } });

And here is my QueryUtils code:

 `String url = ""; try { JSONObject root = new JSONObject(SAMPLE_JSON_RESPONSE); JSONArray earthquakesArray = root.getJSONArray("features"); JSONObject currentEarthquake = earthquakesArray.getJSONObject(arrayNum); JSONObject propertires = currentEarthquake.getJSONObject("properties"); url = propertires.getString("url"); Log.v("JSON", "url" + url); }catch (JSONException e) { Log.e("JSON", "Error"); } return url;` 

I've already checked, and the url is working.

Can someone help me?

9
  • 1
    Please edit your question and post the entire Java stack trace, not just the error message. Commented Apr 8, 2018 at 18:16
  • 1
    Doesn't it say anything else? As the program and line number where the npe is being thrown? Commented Apr 8, 2018 at 18:17
  • 1
    Did you added INTERNET permission in manifest? Commented Apr 8, 2018 at 18:18
  • 1
    how do you add INTERNET permission? Commented Apr 8, 2018 at 18:20
  • <uses-permission android:name="android.permission.INTERNET"></uses-permission> Add this in Manifest file after manifest tag. Commented Apr 8, 2018 at 18:22

3 Answers 3

4

Never create an instance of an activity yourself. Only the framework can create an activity successfully.

Get rid of:

EarthquakeActivity ea = new EarthquakeActivity(); 

Instead, if EarthquakeActivity is the activity that hosts the AdapterView that is using EarthquakeAdapter, pass the EarthquakeActivity into the EarthquakeAdapter constructor, so the adapter has access to it.

Sign up to request clarification or add additional context in comments.

Comments

0

Need to add this permission in manifest file

<uses-permission android:name="android.permission.INTERNET"/> 

Comments

0

Your exception is related to startActivityForResult as it stated:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference at android.app.Activity.startActivityForResult(Activity.java:4473) at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54) 

So it could be the problem with something this not with your Implicit intent

And android.app.Fragment is different than android.support.v4.app.Fragment make sure you imported correct one

And also make sure you added INTERNET permission:

To add Internet permission in manifest

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

Add it in after manifest tag in Manifest file

It could be like:

<manifest ... > <uses-permission android:name="android.permission.INTERNET"></uses-permission> ... </manifest> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.