我正在尝试从我的应用中打开浏览器。
继承我的代码:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
但我继续犯这个错误:
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)
这是我的EarthquakeAdapter代码:
currentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = QueryUtils.getURL(positionNum);
EarthquakeActivity ea = new EarthquakeActivity();
ea.startIntent(url);
}
});
这是我的QueryUtils代码:
`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;`
我已经检查过了,网址正在运行。
有人可以帮助我吗?
答案 0 :(得分:4)
从不自己创建活动的实例。只有框架才能成功创建活动。
摆脱:
EarthquakeActivity ea = new EarthquakeActivity();
相反,如果EarthquakeActivity
是托管使用AdapterView
的{{1}}的活动,请将EarthquakeAdapter
传递给EarthquakeActivity
构造函数,以便适配器可以访问它。
答案 1 :(得分:0)
您的异常与startActivityForResult有关,如下所述:
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)
所以这可能是一个问题,而不是你的隐含意图
android.app.Fragment
与android.support.v4.app.Fragment
不同,请确保导入正确的
并确保您添加了INTERNET权限:
在清单
中添加Internet权限<uses-permission android:name="android.permission.INTERNET"></uses-permission>
在清单文件
中的清单标记之后添加它可能就像:
<manifest ... >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
...
</manifest>
答案 2 :(得分:0)
需要在清单文件中添加此权限
<uses-permission android:name="android.permission.INTERNET"/>