我是开发Android应用程序的新手。我一直在这里和其他网站上阅读饲料来学习。当我点击一个名为Social的按钮时,我得到了不幸的是[appname]已停止。我正在构建一个应用程序,这就是我所拥有的:
MainActivity.java
Button btnOut = (Button)findViewById(R.id.btnOut);
btnOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog alertbox = new Dialog(MainActivity.this);
alertbox.setContentView(R.layout.button);
alertbox.setTitle("News - Choose One:");
Button world = (Button) alertbox.findViewById(R.id.world);
world.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WebView myWebView = (WebView) findViewById(R.id.webview2);
myWebView.setWebViewClient(new WebViewClient());
setContentView(myWebView);
myWebView.loadUrl("http://news.yahoo.com/world");
}
});
Button tech = (Button) alertbox.findViewById(R.id.tech);
tech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WebView myWebView = (WebView) findViewById(R.id.webview2);
myWebView.setWebViewClient(new WebViewClient());
setContentView(myWebView);
myWebView.loadUrl("http://news.yahoo.com/tech");
}
});
Button sport = (Button) alertbox.findViewById(R.id.sport);
sport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WebView myWebView = (WebView) findViewById(R.id.webview2);
myWebView.setWebViewClient(new WebViewClient());
setContentView(myWebView);
myWebView.loadUrl("http://sports.yahoo.com");
}
});
alertbox.show();
}
});
WebActivity2.java
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview2" >
</WebView>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nicx.amazing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission
android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</manifest>
编辑#2(抱歉我之前放错了logcat):
logcat的
04-09 20:04:40.348: W/dalvikvm(26501): threadid=1: thread exiting with uncaught exception (group=0x40abc210)
04-09 20:04:40.378: E/AndroidRuntime(26501): FATAL EXCEPTION: main
04-09 20:04:40.378: E/AndroidRuntime(26501): java.lang.NullPointerException
04-09 20:04:40.378: E/AndroidRuntime(26501): at com.nicx.amazing.MainActivity$3$1.onClick(MainActivity.java:127)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.view.View.performClick(View.java:3574)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.view.View$PerformClick.run(View.java:14293)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.os.Handler.handleCallback(Handler.java:605)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.os.Looper.loop(Looper.java:137)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.app.ActivityThread.main(ActivityThread.java:4448)
04-09 20:04:40.378: E/AndroidRuntime(26501): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 20:04:40.378: E/AndroidRuntime(26501): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 20:04:40.378: E/AndroidRuntime(26501): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-09 20:04:40.378: E/AndroidRuntime(26501): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-09 20:04:40.378: E/AndroidRuntime(26501): at dalvik.system.NativeStart.main(Native Method)
04-09 20:04:40.348: W/dalvikvm(26501): threadid=1: thread exiting with uncaught exception (group=0x40abc210)
04-09 20:04:40.378: E/AndroidRuntime(26501): FATAL EXCEPTION: main
04-09 20:04:40.378: E/AndroidRuntime(26501): java.lang.NullPointerException
04-09 20:04:40.378: E/AndroidRuntime(26501): at com.nicx.amazing.MainActivity$3$1.onClick(MainActivity.java:127)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.view.View.performClick(View.java:3574)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.view.View$PerformClick.run(View.java:14293)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.os.Handler.handleCallback(Handler.java:605)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.os.Looper.loop(Looper.java:137)
04-09 20:04:40.378: E/AndroidRuntime(26501): at android.app.ActivityThread.main(ActivityThread.java:4448)
04-09 20:04:40.378: E/AndroidRuntime(26501): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 20:04:40.378: E/AndroidRuntime(26501): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 20:04:40.378: E/AndroidRuntime(26501): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-09 20:04:40.378: E/AndroidRuntime(26501): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-09 20:04:40.378: E/AndroidRuntime(26501): at dalvik.system.NativeStart.main(Native Method)
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
这是你的罪魁祸首:
04-09 20:04:40.378: E/AndroidRuntime(26501): java.lang.NullPointerException
04-09 20:04:40.378: E/AndroidRuntime(26501): at com.nicx.amazing.MainActivity$3$1.onClick(MainActivity.java:127)
在MainActivity.java的第127行中,您获得了null
参数值。