我正在尝试修改Android教程(http://developer.android.com/training/basics/firstapp/index.html),以便不是显示用户输入应用的文本,而是加载他们输入的网址并将其显示在WebView中。
这是我的catlog ......
D/dalvikvm( 401): GC_CONCURRENT freed 431K, 6% free 9693K/10311K, paused 474ms+125ms, total 7912ms
D/myfirstapp_tag( 1090): Main Line 23
D/myfirstapp_tag( 1090): Main Line 26
D/myfirstapp_tag( 1090): Main Line 28
D/myfirstapp_tag( 1090): Main Line 30
D/myfirstapp_tag( 1090): Main Line 32
I/ActivityManager( 150): START {cmp=com.example.myfirstapp/.DisplayMessageActivity (has extras) u=0} from pid 1090
W/WindowManager( 150): Failure taking screenshot for (246x410) to layer 21015
I/Choreographer( 1090): Skipped 137 frames! The application may be doing too much work on its main thread.
D/myfirstapp_tag( 1090): Line 14
D/myfirstapp_tag( 1090): Line 16
D/myfirstapp_tag( 1090): Line 20
D/myfirstapp_tag( 1090): Line 22
D/myfirstapp_tag( 1090): Line 32
D/myfirstapp_tag( 1090): Line 34
D/AndroidRuntime( 1090): Shutting down VM
W/dalvikvm( 1090): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
E/AndroidRuntime( 1090): FATAL EXCEPTION: main
E/AndroidRuntime( 1090): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example .myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
E/AndroidRuntime( 1090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
E/AndroidRuntime( 1090): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime( 1090): at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime( 1090): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime( 1090): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1090): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 1090): at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime( 1090): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1090): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 1090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime( 1090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 1090): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1090): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 1090): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:36)
E/AndroidRuntime( 1090): at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime( 1090): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime( 1090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
E/AndroidRuntime( 1090): ... 11 more
W/ActivityManager( 150): Force finishing activity com.example.myfirstapp/.DisplayMessageActivity
W/ActivityManager( 150): Force finishing activity com.example.myfirstapp/.MainActivity
W/ActivityManager( 150): Activity pause timeout for ActivityRecord{415e1cc8 com.example.myfirstapp/.DisplayMessageActivity}
我在ubuntu上执行此命令行方式,所以这是我的./AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MainActivity" 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=".DisplayMessageActivity" android:label="@string/title_activity_display_message" >
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
这是我的res / layout / main.xml ...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
...和我的res / layout / webview.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
我有两个源文件。这一个是src / com / example / myfirstapp / MainActivity.java
package com.example.myfirstapp;
import android.app.Activity;
import android.view.View;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.util.Log;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d ("myfirstapp_tag", "Main Line 15" );
super.onCreate(savedInstanceState);
Log.d ("myfirstapp_tag", "Main Line 17" );
setContentView(R.layout.main);
Log.d ("myfirstapp_tag", "Main Line 19" );
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Log.d ("myfirstapp_tag", "Main Line 23" );
// Do something in response to button
Intent intent = new Intent(this, com.example.myfirstapp.DisplayMessageActivity.class);
Log.d ("myfirstapp_tag", "Main Line 26" );
EditText editText = (EditText) findViewById(R.id.edit_message);
Log.d ("myfirstapp_tag", "Main Line 28" );
String message = editText.getText().toString();
Log.d ("myfirstapp_tag", "Main Line 30" );
intent.putExtra(EXTRA_MESSAGE, message);
Log.d ("myfirstapp_tag", "Main Line 32" );
startActivity(intent);
}
}
这一个... src / com / example / myfirstapp / DisplayMessageActivity.java
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
import android.view.View;
import android.webkit.WebView;
import android.os.Bundle;
import android.util.Log;
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d ("myfirstapp_tag", "Line 14" );
super.onCreate(savedInstanceState);
Log.d ("myfirstapp_tag", "Line 16" );
// Get the message from the intent
Intent intent = getIntent();
Log.d ("myfirstapp_tag", "Line 20" );
String message = intent.getStringExtra(com.example.myfirstapp.MainActivity.EXTRA_MESSAGE);
Log.d ("myfirstapp_tag", "Line 22" );
// Create the text view
// TextView textView = new TextView(this);
// textView.setTextSize(40);
// textView.setText(message);
// Set the text view as the activity layout
// setContentView(textView);
//WebView webView = new WebView(this); //WebView myWebView = (WebView) findViewById(R.id.webview);
Log.d ("myfirstapp_tag", "Line 32" );
WebView webView = (WebView) findViewById(R.id.webview); //webView = (WebView) findViewById(R.id.webview);
Log.d ("myfirstapp_tag", "Line 34" );
//setContentView(webView);
webView.loadUrl(message);
Log.d ("myfirstapp_tag", "Line 37" );
setContentView(webView);
Log.d ("myfirstapp_tag", "Line 39" );
}
}
我可以从catlog上看到这条线......
WebView webView = (WebView) findViewById(R.id.webview);
...是我的应用程序即将死亡的地方。我只是不明白为什么。有人可以帮助我:))
答案 0 :(得分:3)
你必须调用setContentView(R.layout.xmlfile);在调用此行之前,
WebView webView = (WebView) findViewById(R.id.webview);
这是因为您尝试引用已在xml文件中分配的资源。
但是Android没有将资源映射到xml文件就无法找到资源。
因此,只有当您使用xml文件调用setContentView时,andorid才会尝试使用您在WebView
中指定的ID查找findViewByID()
的特定xml文件。如果不是它肯定会返回NPException。
或者,如果您不想使用xml分配的资源,请执行此操作,
WebView webView = new WebView(this);
webView.loadUrl(message);
setContentView(webView);
答案 1 :(得分:2)
你忘了打电话了
setContentView(R.layout.webview);
答案 2 :(得分:1)
而不是
WebView webView = (WebView) findViewById(R.id.webview);
将其更改为
WebView webView = new WebView(this);
然后致电
setContentView(w);
或者
your_layout.xml
中的应包含webview
<{1>}onCreate()
DisplayActivity
这应该先调用
setContentView(R.layout.your_layout);
然后你可以打电话
WebView webView = (WebView) findViewById(R.id.webview);
现在您可以在WebView
对象