WebView的问题,运行报错!
seahi 2015-07-16 04:38:11 小弟初学android开发,第一个程序就遇到问题,请各位大大帮忙啊。
我的程序很简单,就是放置一个WebView载入一个网页。
代码如下:
-------------------------------------MainActivity.java---------------------------------------------
package com.example;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private WebView myWebView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
// 打开网页
myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://192.168.1.13/an/mobile/producechecktvlist.aspx");// 百度链接
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// myWebView.setWebViewClient(new MyWebViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
-------------------------manifest加入
<uses-permission android:name="android.permission.INTERNET" />
---------------Console输入如下:
[2015-07-16 16:29:28 - 1] Android Launch!
[2015-07-16 16:29:28 - 1] adb is running normally.
[2015-07-16 16:29:28 - 1] Performing com.example.MainActivity activity launch
[2015-07-16 16:29:28 - 1] Automatic Target Mode: using device '1a2253f'
[2015-07-16 16:29:29 - 1] Application already deployed. No need to reinstall.
[2015-07-16 16:29:29 - 1] Starting activity com.example.MainActivity on device 1a2253f
[2015-07-16 16:29:30 - 1] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example/.MainActivity }
---------------LogCat输入如下:
07-16 16:30:20.300: D/ActivityThread(16372): handleBindApplication:com.example
07-16 16:30:20.300: D/ActivityThread(16372): setTargetHeapUtilization:0.75
07-16 16:30:20.300: D/ActivityThread(16372): setTargetHeapMinFree:2097152
07-16 16:30:20.350: V/ActivityThread(16372): freeHardwareCacheIfNeeded,configDiff=0 force=true
07-16 16:30:20.350: V/ActivityThread(16372): freeHardwareCacheIfNeeded,hasFontScaleConfigChange=false
07-16 16:30:20.450: D/AndroidRuntime(16372): Shutting down VM
07-16 16:30:20.450: W/dalvikvm(16372): threadid=1: thread exiting with uncaught exception (group=0x41ee4d88)
07-16 16:30:20.450: E/AndroidRuntime(16372): FATAL EXCEPTION: main
07-16 16:30:20.450: E/AndroidRuntime(16372): Process: com.example, PID: 16372
07-16 16:30:20.450: E/AndroidRuntime(16372): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.NullPointerException
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2196)
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.app.ActivityThread.access$800(ActivityThread.java:140)
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.os.Looper.loop(Looper.java:136)
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.app.ActivityThread.main(ActivityThread.java:5143)
07-16 16:30:20.450: E/AndroidRuntime(16372): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:30:20.450: E/AndroidRuntime(16372): at java.lang.reflect.Method.invoke(Method.java:515)
07-16 16:30:20.450: E/AndroidRuntime(16372): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-16 16:30:20.450: E/AndroidRuntime(16372): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
07-16 16:30:20.450: E/AndroidRuntime(16372): at dalvik.system.NativeStart.main(Native Method)
07-16 16:30:20.450: E/AndroidRuntime(16372): Caused by: java.lang.NullPointerException
07-16 16:30:20.450: E/AndroidRuntime(16372): at com.example.MainActivity.onCreate(MainActivity.java:34)
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.app.Activity.performCreate(Activity.java:5356)
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1089)
07-16 16:30:20.450: E/AndroidRuntime(16372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2160)
07-16 16:30:20.450: E/AndroidRuntime(16372): ... 11 more
请大家帮忙看看是什么问题啊?