2012-10-24 49 views
1

我創建了一個程序應該連接到互聯網的android應用程序,當我按下Go按鈕時,程序應該打開webView頁面(google.com)。我的程序無法連接到互聯網,我不知道爲什麼?我將log.i()函數添加到程序中,當我點擊按鈕時,我看到了這個。爲什麼我的程序無法打開webView頁面?

Go.java

package org.example.test; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.view.View.OnClickListener; 

public class MainActivity extends Activity{ 
private WebView webView; 
private Button go_button; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    Log.i(UI_MODE_SERVICE, "HELOOOOOOOOOOOOOOOOOO"); 
    setContentView(R.layout.activity_main); 
    webView=(WebView) findViewById(R.id.web_view); 
    go_button=(Button) findViewById(R.id.go_button); 
    go_button.setOnClickListener(new OnClickListener(){ 
     public void onClick(View view){ 
      Log.i(UI_MODE_SERVICE, "STARRRRRRRRRRRRRRT"); 
      webView.loadUrl("http://www.google.com"); 
      Log.i(UI_MODE_SERVICE, "BYEEEEEEEEEEEEEEEEEEEEE"); 
     } 
    }); 
} 
} 

MainActivity:

package org.example.test; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <WebView 
     android:id="@+id/web_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1.0" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="bottom" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/go_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/go_button"/> 
     </LinearLayout> 
    </LinearLayout> 

的strings.xml

<resources> 

    <string name="app_name">test</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="menu_settings">Settings</string> 
    <string name="title_activity_main">MainActivity</string> 

    <string name="go_button">GO</string> 

</resources> 

AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="org.example.test" 
    android:versionCode="1" 
    android:versionName="1.0" > 
<uses-sdk 
    android:minSdkVersion="9" 
    android:targetSdkVersion="15" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

的logcat:

10-24 22:04:50.674: DEBUG/AndroidRuntime(30445): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<< 
10-24 22:04:50.674: DEBUG/AndroidRuntime(30445): CheckJNI is OFF 
10-24 22:04:50.854: DEBUG/AndroidRuntime(30445): Calling main entry com.android.commands.pm.Pm 
10-24 22:04:50.870: DEBUG/AndroidRuntime(30445): Shutting down VM 
10-24 22:04:50.870: INFO/AndroidRuntime(30445): NOTE: attach of thread 'Binder Thread #3' failed 
10-24 22:04:50.877: DEBUG/dalvikvm(30445): GC_CONCURRENT freed 101K, 89% free 463K/4096K, paused 0ms+1ms 
10-24 22:04:50.885: DEBUG/jdwp(30445): Got wake-up signal, bailing out of select 
10-24 22:04:50.885: DEBUG/dalvikvm(30445): Debugger has detached; object registry had 1 entries 
10-24 22:04:51.213: DEBUG/AndroidRuntime(30458): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<< 
10-24 22:04:51.213: DEBUG/AndroidRuntime(30458): CheckJNI is OFF 
10-24 22:04:51.393: DEBUG/AndroidRuntime(30458): Calling main entry com.android.commands.am.Am 
10-24 22:04:51.393: INFO/ActivityManager(178): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.example.test/.MainActivity} from pid 30458 
10-24 22:04:51.495: DEBUG/AndroidRuntime(30458): Shutting down VM 
10-24 22:04:51.510: DEBUG/dalvikvm(30469): Late-enabling CheckJNI 
10-24 22:04:51.510: INFO/ActivityManager(178): Start proc org.example.test for activity org.example.test/.MainActivity: pid=30469 uid=10111 gids={} 
10-24 22:04:51.518: DEBUG/dalvikvm(30458): GC_CONCURRENT freed 102K, 89% free 484K/4096K, paused 1ms+0ms 
10-24 22:04:51.518: DEBUG/dalvikvm(30458): Debugger has detached; object registry had 1 entries 
10-24 22:04:51.518: INFO/AndroidRuntime(30458): NOTE: attach of thread 'Binder Thread #3' failed 
10-24 22:04:51.526: INFO/WindowManager(178): createSurface Window{41cc2288 Starting org.example.test paused=false}: DRAW NOW PENDING 
10-24 22:04:51.573: DEBUG/OpenGLRenderer(438): Flushing caches (mode 1) 
10-24 22:04:51.580: INFO/uimode(30469): HELOOOOOOOOOOOOOOOOOO 
10-24 22:04:51.635: DEBUG/OpenGLRenderer(438): Flushing caches (mode 0) 
10-24 22:04:51.666: INFO/WindowManager(178): createSurface Window{41cc2e48 org.example.test/org.example.test.MainActivity paused=false}: DRAW NOW PENDING 
10-24 22:04:51.690: DEBUG/libEGL(30469): loaded /system/lib/egl/libGLES_android.so 
10-24 22:04:51.698: DEBUG/libEGL(30469): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 
10-24 22:04:51.698: DEBUG/libEGL(30469): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 
10-24 22:04:51.705: DEBUG/libEGL(30469): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 
10-24 22:04:51.854: DEBUG/OpenGLRenderer(30469): Enabling debug mode 0 
10-24 22:04:52.260: INFO/ActivityManager(178): Displayed org.example.test/.MainActivity: +767ms 
10-24 22:05:00.276: INFO/uimode(30469): STARRRRRRRRRRRRRRT 
10-24 22:05:00.276: INFO/uimode(30469): BYEEEEEEEEEEEEEEEEEEEEE 
10-24 22:05:03.659: INFO/uimode(30469): STARRRRRRRRRRRRRRT 
10-24 22:05:03.659: INFO/uimode(30469): BYEEEEEEEEEEEEEEEEEEEEE 

謝謝提醒。 乾杯。

回答

0

1)您的堆棧回溯很潦草:我沒有看到,如果有一個錯誤

2)感謝您,包括您的AndroidManifest.xml。我沒有看到「互聯網」權限 - 你需要它!

+0

是的。我忘了寫網絡權限manifest.I添加到androidManifest和程序runed並打開webView頁面:)但是當我在我的程序中稍微改變,該程序不打開webView頁面。爲什麼?我複製MainActivity並粘貼到org.example.test中的Go.java。我編輯第一篇文章。請你看看,並告訴我什麼是我的問題?!如果你需要LogCat,請告訴我。謝謝 –

+0

沒有人在這裏!! ??請幫助我,我無法解決我的問題,我累了。謝謝。 –

+0

嘗試在loadUrl()之前添加它:'setContentView(webview);'。您的原始LogCat顯示您*已成功輸入您的「onClick()」,並且您沒有得到任何例外。我猜測問題是你只是沒有顯示瀏覽器。 「setContentView()」應該幫助:) – paulsm4

1

您需要在您的Manifest中添加INTERNET權限。

相關問題