我正在构建我的第一个应用程序之一,这次我使用的是Android Studio 1.5。
该应用程序是一个非常简单的Web浏览器,我已经为此做了很多修复。一切都工作正常,直到我决定按“输入”键比点击一个愚蠢的“go”按钮更简单,更方便。
哦,男孩不是我错了
该应用程序构建良好,但一旦我将其启动到我的手机,它会立即崩溃。
下面是我的MainActivity.java,下面是生成的异常。
正如您可能看到的那样,我已经广泛检查了“null”对象属性,并且错误仍然存在
- CODE -
package eu.depa.browsing.stack;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements OnKeyListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //what .xml it's tied to
//getSupportActionBar().hide(); //hide ugly ass fat bar
//TODO fix exception generated by this
WebView webview = (WebView) findViewById(R.id.webView); //actual web window
EditText toptextbar = (EditText) findViewById(R.id.toptextbar); //address bar
webview.getSettings().setJavaScriptEnabled(true); // Enable javascript
webview.setWebChromeClient(new WebChromeClient()); // Set WebView client
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
WebView webview = (WebView) findViewById(R.id.webView); //actual web window
EditText toptextbar = (EditText) findViewById(R.id.toptextbar); //address bar
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (v != null && event != null && toptextbar.getText() != null && keyCode == KeyEvent.KEYCODE_ENTER){
String text = String.valueOf(toptextbar.getText());
if (!(text.contains(" ")) && text.contains(".")) {
if (text.split("//")[0].equals("http:") || text.split("//")[0].equals("https:"))
webview.loadUrl(text);
else
webview.loadUrl("http://" + text);
} else {
String search = "";
String[] parts = text.split(" ");
for (String item : parts)
search = search + item + "%20";
webview.loadUrl("http://duckduckgo.com/" + search);
}
toptextbar.setText(webview.getUrl());
}
return false;
}
}
- EXCEPTION--我甚至认为这不是我应该提供的
12-17 14:12:15.291 19699-19699/eu.depa.browsing.stack E/AndroidRuntime: FATAL EXCEPTION: main
Process: eu.depa.browsing.stack, PID: 19699
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{eu.depa.browsing.stack/eu.depa.browsing.stack.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5308)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2129)
at eu.depa.browsing.stack.MainActivity.<init>(MainActivity.java:37)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5308)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
答案 0 :(得分:3)
删除以下字段初始化代码,在调用findViewById()
之前无法调用onCreate()
:
WebView webview = (WebView) findViewById(R.id.webView); //actual web window
EditText toptextbar = (EditText) findViewById(R.id.toptextbar); //address bar
字段初始化代码是指onCreate()
和onKey()
方法之间的代码。离开田地&#39;声明:
private WebView webview;
private EditText toptextbar;
答案 1 :(得分:0)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
检查您提供的ID,您确定它们存在于XML中吗?
特别检查webview为第37行:
at eu.depa.browsing.stack.MainActivity.<init>(MainActivity.java:37)
答案 2 :(得分:0)
您的xml文件应如下所示:
webview xml id 必须 webView :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<WebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></WebView>
</RelativeLayout>
希望这会对你有所帮助。