我的项目有两个问题: - webview在第二个活动中不起作用 - 当按下第一个按钮时没有显示消息,而在第二个按下的按钮效果很好..
我有2个活动,第一个有一个按钮,点击时必须打开我项目中命名的新活动:DisplayMessageActivity。这非常好。
我想在第二个活动中调用webview来查看网页并与之交互。但是单击按钮时应用程序停止。问题似乎在于:
WebView myWebView =(WebView)findViewById(R.id.webview); myWebView.loadUrl( “http://www.google.com”);
MainActivity是:
package com.blabla.quiztest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@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();
}
}
public void setupMessageButton(View view) {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Do something in response to button click
Context context = getApplicationContext();
CharSequence text = "Please wait...";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
startActivity(intent);
}
});
}
@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,
`enter code here` Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
SecondAcrivity(DisplayMessageActivity.java)是:
package com.blabla.quiztest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.com");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, 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_display_message,
container, false);
return rootView;
}
}
}
我还在Android Manifest中添加了以下行,详情请参阅:http://goo.gl/li6c4A
> <uses-permission android:name="android.permission.INTERNET"/>
我还添加了这个
> <WebView xmlns:android="http://schemas.android.com/apk/res/android"
> android:id="@+id/webview"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"/>
到名为fragment_display_message.xml的SecondActivity
这是fragment_main.xml(第一个活动):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.blabla.quiztest.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="189dp"
android:onClick="setupMessageButton"
android:text="Button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="60dp"
android:text="@string/hello_world" />
</RelativeLayout>
我的LOG CAT:
05-11 07:52:41.817: D/(1193): HostConnection::get() New Host Connection established 0xb8835450, tid 1193
05-11 07:52:42.007: W/EGL_emulation(1193): eglSurfaceAttrib not implemented
05-11 07:52:42.027: D/OpenGLRenderer(1193): Enabling debug mode 0
05-11 07:52:53.757: D/dalvikvm(1193): GC_FOR_ALLOC freed 127K, 6% free 3258K/3452K, paused 47ms, total 89ms
05-11 07:52:54.247: V/WebViewChromium(1193): Binding Chromium to the background looper Looper (main, tid 1) {b1cf0758}
05-11 07:52:54.347: I/chromium(1193): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
05-11 07:52:54.387: I/BrowserProcessMain(1193): Initializing chromium process, renderers=0
05-11 07:52:55.437: W/chromium(1193): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
05-11 07:52:55.927: D/dalvikvm(1193): GC_FOR_ALLOC freed 80K, 5% free 3388K/3552K, paused 81ms, total 84ms
05-11 07:52:55.937: I/dalvikvm-heap(1193): Grow heap (frag case) to 4.448MB for 1127536-byte allocation
05-11 07:52:56.567: D/dalvikvm(1193): GC_FOR_ALLOC freed 1K, 4% free 4488K/4656K, paused 458ms, total 458ms
05-11 07:52:56.957: I/Choreographer(1193): Skipped 206 frames! The application may be doing too much work on its main thread.
05-11 07:52:57.367: W/EGL_emulation(1193): eglSurfaceAttrib not implemented
05-11 07:52:57.507: W/AwContents(1193): nativeOnDraw failed; clearing to background color.
05-11 07:52:58.077: W/AwContents(1193): nativeOnDraw failed; clearing to background color.
05-11 07:52:58.657: W/AwContents(1193): nativeOnDraw failed; clearing to background color.
05-11 07:52:58.677: W/AwContents(1193): nativeOnDraw failed; clearing to background color.
05-11 08:25:54.197: D/(1260): HostConnection::get() New Host Connection established 0xb8835450, tid 1260
05-11 08:25:54.567: W/EGL_emulation(1260): eglSurfaceAttrib not implemented
05-11 08:25:54.587: D/OpenGLRenderer(1260): Enabling debug mode 0
05-11 08:27:13.397: D/dalvikvm(1260): GC_FOR_ALLOC freed 131K, 6% free 3254K/3452K, paused 380ms, total 406ms
05-11 08:27:13.627: D/AndroidRuntime(1260): Shutting down VM
05-11 08:27:13.627: W/dalvikvm(1260): threadid=1: thread exiting with uncaught exception (group=0xb1a1cba8)
05-11 08:27:13.637: E/AndroidRuntime(1260): FATAL EXCEPTION: main
05-11 08:27:13.637: E/AndroidRuntime(1260): Process: com.blabla.quiztest, PID: 1260
05-11 08:27:13.637: E/AndroidRuntime(1260): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabla.quiztest/com.blabla.quiztest.DisplayMessageActivity}: java.lang.NullPointerException
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.os.Handler.dispatchMessage(Handler.java:102)
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.os.Looper.loop(Looper.java:136)
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-11 08:27:13.637: E/AndroidRuntime(1260): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 08:27:13.637: E/AndroidRuntime(1260): at java.lang.reflect.Method.invoke(Method.java:515)
05-11 08:27:13.637: E/AndroidRuntime(1260): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-11 08:27:13.637: E/AndroidRuntime(1260): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-11 08:27:13.637: E/AndroidRuntime(1260): at dalvik.system.NativeStart.main(Native Method)
05-11 08:27:13.637: E/AndroidRuntime(1260): Caused by: java.lang.NullPointerException
05-11 08:27:13.637: E/AndroidRuntime(1260): at com.blabla.quiztest.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:25)
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.app.Activity.performCreate(Activity.java:5231)
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-11 08:27:13.637: E/AndroidRuntime(1260): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-11 08:27:13.637: E/AndroidRuntime(1260): ... 11 more
05-11 08:27:18.457: I/Process(1260): Sending signal. PID: 1260 SIG: 9
05-11 09:17:54.547: D/(1313): HostConnection::get() New Host Connection established 0xb8835450, tid 1313
05-11 09:17:54.667: W/EGL_emulation(1313): eglSurfaceAttrib not implemented
05-11 09:17:54.697: D/OpenGLRenderer(1313): Enabling debug mode 0
05-11 09:33:07.107: D/dalvikvm(1313): GC_FOR_ALLOC freed 135K, 6% free 3251K/3452K, paused 390ms, total 390ms
05-11 09:33:07.447: D/AndroidRuntime(1313): Shutting down VM
05-11 09:33:07.447: W/dalvikvm(1313): threadid=1: thread exiting with uncaught exception (group=0xb1a1cba8)
05-11 09:33:07.507: E/AndroidRuntime(1313): FATAL EXCEPTION: main
05-11 09:33:07.507: E/AndroidRuntime(1313): Process: com.blabla.quiztest, PID: 1313
05-11 09:33:07.507: E/AndroidRuntime(1313): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabla.quiztest/com.blabla.quiztest.DisplayMessageActivity}: java.lang.NullPointerException
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.os.Handler.dispatchMessage(Handler.java:102)
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.os.Looper.loop(Looper.java:136)
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-11 09:33:07.507: E/AndroidRuntime(1313): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 09:33:07.507: E/AndroidRuntime(1313): at java.lang.reflect.Method.invoke(Method.java:515)
05-11 09:33:07.507: E/AndroidRuntime(1313): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-11 09:33:07.507: E/AndroidRuntime(1313): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-11 09:33:07.507: E/AndroidRuntime(1313): at dalvik.system.NativeStart.main(Native Method)
05-11 09:33:07.507: E/AndroidRuntime(1313): Caused by: java.lang.NullPointerException
05-11 09:33:07.507: E/AndroidRuntime(1313): at com.blabla.quiztest.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:25)
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.app.Activity.performCreate(Activity.java:5231)
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-11 09:33:07.507: E/AndroidRuntime(1313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-11 09:33:07.507: E/AndroidRuntime(1313): ... 11 more
05-11 09:33:12.327: I/Process(1313): Sending signal. PID: 1313 SIG: 9
05-11 09:40:08.817: D/(1368): HostConnection::get() New Host Connection established 0xb8920268, tid 1368
05-11 09:40:08.967: W/EGL_emulation(1368): eglSurfaceAttrib not implemented
05-11 09:40:08.987: D/OpenGLRenderer(1368): Enabling debug mode 0
05-11 09:40:38.187: D/dalvikvm(1368): GC_FOR_ALLOC freed 138K, 6% free 3247K/3452K, paused 158ms, total 181ms
05-11 09:40:38.657: D/AndroidRuntime(1368): Shutting down VM
05-11 09:40:38.657: W/dalvikvm(1368): threadid=1: thread exiting with uncaught exception (group=0xb1a1cba8)
05-11 09:40:38.787: E/AndroidRuntime(1368): FATAL EXCEPTION: main
05-11 09:40:38.787: E/AndroidRuntime(1368): Process: com.blabla.quiztest, PID: 1368
05-11 09:40:38.787: E/AndroidRuntime(1368): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabla.quiztest/com.blabla.quiztest.DisplayMessageActivity}: java.lang.NullPointerException
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.os.Handler.dispatchMessage(Handler.java:102)
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.os.Looper.loop(Looper.java:136)
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-11 09:40:38.787: E/AndroidRuntime(1368): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 09:40:38.787: E/AndroidRuntime(1368): at java.lang.reflect.Method.invoke(Method.java:515)
05-11 09:40:38.787: E/AndroidRuntime(1368): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-11 09:40:38.787: E/AndroidRuntime(1368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-11 09:40:38.787: E/AndroidRuntime(1368): at dalvik.system.NativeStart.main(Native Method)
05-11 09:40:38.787: E/AndroidRuntime(1368): Caused by: java.lang.NullPointerException
05-11 09:40:38.787: E/AndroidRuntime(1368): at com.blabla.quiztest.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:25)
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.app.Activity.performCreate(Activity.java:5231)
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-11 09:40:38.787: E/AndroidRuntime(1368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-11 09:40:38.787: E/AndroidRuntime(1368): ... 11 more
05-11 09:40:41.807: I/Process(1368): Sending signal. PID: 1368 SIG: 9
05-11 09:53:59.507: D/(1425): HostConnection::get() New Host Connection established 0xb89202e8, tid 1425
05-11 09:53:59.657: W/EGL_emulation(1425): eglSurfaceAttrib not implemented
05-11 09:53:59.667: D/OpenGLRenderer(1425): Enabling debug mode 0
05-11 09:54:19.777: D/dalvikvm(1425): GC_FOR_ALLOC freed 142K, 7% free 3243K/3452K, paused 496ms, total 506ms
05-11 09:54:20.567: D/AndroidRuntime(1425): Shutting down VM
05-11 09:54:20.567: W/dalvikvm(1425): threadid=1: thread exiting with uncaught exception (group=0xb1a1cba8)
05-11 09:54:20.607: E/AndroidRuntime(1425): FATAL EXCEPTION: main
05-11 09:54:20.607: E/AndroidRuntime(1425): Process: com.blabla.quiztest, PID: 1425
05-11 09:54:20.607: E/AndroidRuntime(1425): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabla.quiztest/com.blabla.quiztest.DisplayMessageActivity}: java.lang.NullPointerException
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.os.Handler.dispatchMessage(Handler.java:102)
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.os.Looper.loop(Looper.java:136)
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-11 09:54:20.607: E/AndroidRuntime(1425): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 09:54:20.607: E/AndroidRuntime(1425): at java.lang.reflect.Method.invoke(Method.java:515)
05-11 09:54:20.607: E/AndroidRuntime(1425): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-11 09:54:20.607: E/AndroidRuntime(1425): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-11 09:54:20.607: E/AndroidRuntime(1425): at dalvik.system.NativeStart.main(Native Method)
05-11 09:54:20.607: E/AndroidRuntime(1425): Caused by: java.lang.NullPointerException
05-11 09:54:20.607: E/AndroidRuntime(1425): at com.blabla.quiztest.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:27)
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.app.Activity.performCreate(Activity.java:5231)
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-11 09:54:20.607: E/AndroidRuntime(1425): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-11 09:54:20.607: E/AndroidRuntime(1425): ... 11 more