我是Android开发的新手,我正在制作一个基本的网络浏览器,并在我的手机上测试我的应用程序,该消息显示不幸的应用程序停止工作并且没有打开应用程序。
我发布了我已创建的代码文件
这是java类
package com.example.dell.browser;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends Activity {
public String inputURL = "https://www.google.co.in/";
Button searchButton = (Button)findViewById(R.id.searchButton);
ImageButton googleButton = (ImageButton)findViewById(R.id.googleButton);
ImageButton youtubeButton = (ImageButton)findViewById(R.id.youtubeButton);
ImageButton facebookButton = (ImageButton)findViewById(R.id.facebookButton);
ImageButton wikiButton = (ImageButton)findViewById(R.id.wikiButton);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
inputURL = ((EditText)findViewById(R.id.urlText)).getText().toString();
// Calling the webView Intent
Intent webPage = new Intent(MainActivity.this, WebPage.class);
webPage.putExtra("searchedURL", inputURL);
webPage.putExtra("hint", "searchButton");
startActivity(webPage);
}
});
googleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Calling the webView Intent with google homepage URL
Intent googlePage = new Intent(MainActivity.this, WebPage.class);
googlePage.putExtra("RequestedURL", "https://www.google.co.in");
googlePage.putExtra("hint", "imageButton");
startActivity(googlePage);
}
});
youtubeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Calling webView Intent with youtube URL
Intent youtuePage = new Intent(MainActivity.this, WebPage.class);
youtuePage.putExtra("RequestedURL", "https://www.youtube.com");
youtuePage.putExtra("hint", "imageButton");
startActivity(youtuePage);
}
});
facebookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Calling webView Intent with facebook URL
Intent facebookPage = new Intent(MainActivity.this, WebPage.class);
facebookPage.putExtra("RequestedURL", "https://www.facebook.com");
facebookPage.putExtra("hint", "imageButton");
startActivity(facebookPage);
}
});
wikiButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Calling webView Intent with wikipedia URL
Intent wikiPage = new Intent(MainActivity.this, WebPage.class);
wikiPage.putExtra("RequestedURL", "https://en.wikipedia.org");
wikiPage.putExtra("hint", "imageButton");
startActivity(wikiPage);
}
});
}
}
这是WebView Activity的java类
package com.example.dell.browser;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPage extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle urlrecieved = getIntent().getExtras();
Bundle searchTypeURL = getIntent().getExtras();
Bundle hintRecieved = getIntent().getExtras();
if (urlrecieved==null)
return;
String enteredURL = urlrecieved.getString("RequestedURL");
String searchURL = searchTypeURL.getString("searchedURL");
String clue = hintRecieved.getString("hint");
WebView pageView = findViewById(R.id.webPage_area);
pageView.setWebViewClient(new WebViewClient());
pageView.getSettings().setJavaScriptEnabled(true);
pageView.setVerticalScrollBarEnabled(false);
pageView.setHorizontalScrollBarEnabled(false);
switch (clue){
case "searchButton":
pageView.loadUrl(searchURL);
break;
case "imageButton":
switch (enteredURL){
case "https://www.google.co.in": pageView.loadUrl("https://www.google.co.in");
break;
case "https://www.youtube.com": pageView.loadUrl("https://www.youtube.com");
break;
case "https://www.facebook.com": pageView.loadUrl("https://www.facebook.com");
break;
case "https://en.wikipedia.org": pageView.loadUrl("https://en.wikipedia.org");
break;
}
}
}
}
这就是LogCat所说的
11-08 04:33:55.084 10874-10874/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.browser, PID: 10874
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.dell.browser/com.example.dell.browser.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:2568)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
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:2328)
at com.example.dell.browser.MainActivity.<init>(MainActivity.java:14)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2558)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
11-08 04:33:59.766 10912-10912/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.browser, PID: 10912
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.dell.browser/com.example.dell.browser.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:2568)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
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:2328)
at com.example.dell.browser.MainActivity.<init>(MainActivity.java:14)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2558)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)