2017-06-14 51 views
0

所以我有這個按鈕,我希望它按下按鈕時打開一個webviewer。爲什麼當我按下按鈕時不打開webviewer?

這裏就是按鈕

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<ImageButton 
    android:id="@+id/signIn" 
    android:layout_width="match_parent" 
    android:layout_height="61dp" 
    android:src="@drawable/signin" 
    android:textAlignment="center" /> 

</LinearLayout> 

這裏點擊按鈕

public class SignIn extends Activity { 
ImageView img; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    img = (ImageView) findViewById(R.id.signIn); 
    signIn(); 

} 
public void signIn() { 
    img.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(SignIn.this, WebViewActivity.class); 
      startActivity(intent); 
     } 
    }); 
} 
} 

這裏的類代碼的XML是我有webviwer.xml

<?xml version="1.0" encoding="utf-8"?> 
<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/webView1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" /> 

這裏是webviewer類

public class WebViewActivity extends Activity{ 

private WebView webView; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 

    webView = (WebView) findViewById(R.id.webView1); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl("http://www.google.com"); 
} 
} 

當點擊什麼也沒有發生,我還編輯清單有上網

回答

0

做一個小的修正。在XML中,您使用的是「ImageButton」,但在使用「ImageView」的java文件中。

<ImageView 
android:id="@+id/signIn" 
android:layout_width="match_parent" 
android:layout_height="61dp" 
android:src="@drawable/signin" 
android:textAlignment="center" /> 
0

我認爲用XML ImageButton編寫並在java文件中通過id搜索ImageView是錯誤的。

使用ImageView或ImageButton!

如果您想使用imageView,請不要忘記使其成爲可能。

相關問題