我一直在尝试制作一个小型的WebView应用程序,让用户可以在其中搜索和加载网站。
目前我的代码在结构和使用的引用方面看起来很有希望,但我偶然发现了一个我似乎无法解决的错误。我一直在寻找有关如何将数据作为字符串从我的MainActivity类EditText解析为WebActivity类WebView的解决方案。
到目前为止,我没有运气,我已经尝试了几个网站并对此进行了更多研究,但我不知道发生了什么。我甚至在StackOverFlow上找到了关于我的情况的类似答案,但没有任何对我有用。
这是我得到的错误:
04-20 16:56:55.563: D/AndroidRuntime(32040): Shutting down VM
04-20 16:56:55.564: E/AndroidRuntime(32040): FATAL EXCEPTION: main
04-20 16:56:55.564: E/AndroidRuntime(32040): Process: com.thearclabs.carpo, PID: 32040
04-20 16:56:55.564: E/AndroidRuntime(32040): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thearclabs.carpo/com.thearclabs.carpo.WebActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
04-20 16:56:55.564: E/AndroidRuntime(32040): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
04-20 16:56:55.564: E/AndroidRuntime(32040): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
04-20 16:56:55.564: E/AndroidRuntime(32040): at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-20 16:56:55.564: E/AndroidRuntime(32040): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
04-20 16:56:55.564: E/AndroidRuntime(32040): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 16:56:55.564: E/AndroidRuntime(32040): at android.os.Looper.loop(Looper.java:135)
04-20 16:56:55.564: E/AndroidRuntime(32040): at android.app.ActivityThread.main(ActivityThread.java:5254)
04-20 16:56:55.564: E/AndroidRuntime(32040): at java.lang.reflect.Method.invoke(Native Method)
04-20 16:56:55.564: E/AndroidRuntime(32040): at java.lang.reflect.Method.invoke(Method.java:372)
04-20 16:56:55.564: E/AndroidRuntime(32040): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-20 16:56:55.564: E/AndroidRuntime(32040): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-20 16:56:55.564: E/AndroidRuntime(32040): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
04-20 16:56:55.564: E/AndroidRuntime(32040): at com.thearclabs.carpo.WebActivity.<init>(WebActivity.java:9)
04-20 16:56:55.564: E/AndroidRuntime(32040): at java.lang.reflect.Constructor.newInstance(Native Method)
04-20 16:56:55.564: E/AndroidRuntime(32040): at java.lang.Class.newInstance(Class.java:1606)
04-20 16:56:55.564: E/AndroidRuntime(32040): at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
04-20 16:56:55.564: E/AndroidRuntime(32040): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
04-20 16:56:55.564: E/AndroidRuntime(32040): ... 10 more
这是我的MainActivity的onCreate方法中的代码:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText searchInput = (EditText) findViewById(R.id.searchInput);
final String webUrl = searchInput.getText().toString();
searchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
Intent intent = new Intent(MainActivity.this, WebActivity.class);
intent.putExtra("website", webUrl);
startActivity(intent);
return true;
}
return false;
}
});
这是我的WebActivity代码:
public class WebActivity extends Activity {
String webUrl = getIntent().getExtras().getString("webUrl"); // error is coming from here, according the the error log.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("http://example.com" + webUrl);
// webView.getSettings().setJavaScriptEnabled(true);
}
}
答案 0 :(得分:0)
使用getIntent()
方法而不是类级别的方法调用onCreate
:
String webUrl = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
webUrl = getIntent().getExtras().getString("website");
//..your code here...
}
还要进行以下更改:
1。在访问null
方法之前添加getExtras()
检查。
2. 使用上一个Activity中用于在Intent中添加数据的相同键。目前您正在使用webUrl
获取价值,但之前使用的是活动website
答案 1 :(得分:0)
移动此行
String webUrl = getIntent().getExtras().getString("webUrl");
在onCreate()
方法之后。
修改强> 的
移动此行
final String webUrl = searchInput.getText().toString();
进入这种状况
searchInput.setOnEditorActionListener(new TextView.OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
String webUrl = searchInput.getText().toString();
Intent intent = new Intent(MainActivity.this, WebActivity.class);
intent.putExtra("website", webUrl);
startActivity(intent);
return true;
}
return false;
}
});
答案 2 :(得分:0)
由于getIntent()
方法在活动生命周期方法中可用。因此使用
String webUrl = getIntent().getExtras().getString("website");
//错误来自此处。
在onCreate
方法内
String webUrl = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
webUrl = getIntent().getExtras().getString("website");
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("http://example.com" + webUrl);
// webView.getSettings().setJavaScriptEnabled(true);
}
答案 3 :(得分:0)
你必须使用密钥从意图中获取数据。尝试以下代码
public class WebActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
String webUrl = getIntent().getExtras().getString("website"); // error is coming from here, according the the error log.
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("http://example.com" + webUrl);
// webView.getSettings().setJavaScriptEnabled(true);
}
}
答案 4 :(得分:0)
请在那里更新webActivity。
public class WebActivity extends Activity {
String webUrl="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
webUrl = bundle.getString("webUrl");
}
WebView webView = (WebView) findViewById(R.id.webView);
if(!TextUtils.isEmpty(webUrl)){
webView.loadUrl("http://example.com" + webUrl);
}
// webView.getSettings().setJavaScriptEnabled(true);
}
}