2013-07-18 139 views
0

我一直试图解决这个问题,现在已经搜索了很多天,我也搜遍了这里提供的许多相关问题,但我还没有解决这个问题。Webview将不会加载按钮单击?

当我创建一个单独的项目时,代码可以正常工作,但是由于某些原因它不适用于此。

我有几个可从主页面单击按钮(这部分工作正常)的活动,然后从这些活动中的每一个我都有按钮,点击时应该打开一个网站,但它是没有发生.....

我已经将所有活动添加到清单(我可以看到)看过几次。大声笑

我已经重做了几次代码但无济于事,我没有做的唯一的事情是从头开始项目(最后的手段)。

感谢您的帮助。

** MainActivity.java(主页工作的罚款)

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageButton; 
public class MainActivity extends Activity { 
    ImageButton imagebutton1, imagebutton2,imagebutton3, imagebutton4, 
    imagebutton5, imagebutton6; 

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

    public void addListenerOnButton() { 

     final Context context = this; 

     imagebutton1 = (ImageButton) findViewById(R.id.imageButton1); 

     imagebutton1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Intent intent = new Intent(context, Fashion.class); 
          startActivity(intent); 

      } 
     }); 


      imagebutton2 = (ImageButton) findViewById(R.id.imageButton2); 

      imagebutton2.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 

        Intent intent = new Intent(context, Electrical.class); 
           startActivity(intent); 

       } 

     }); 

      imagebutton3 = (ImageButton) findViewById(R.id.imageButton3); 

      imagebutton3.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 

        Intent intent = new Intent(context, Kids.class); 
           startActivity(intent); 

       } 
      }); 


       imagebutton4 = (ImageButton) findViewById(R.id.imageButton4); 

       imagebutton4.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View arg0) { 

         Intent intent = new Intent(context, Pets.class); 
            startActivity(intent); 

        } 

      }); 

       imagebutton5 = (ImageButton) findViewById(R.id.imageButton5); 

       imagebutton5.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View arg0) { 

         Intent intent = new Intent(context, Food.class); 
            startActivity(intent); 

        } 

       }); 

       imagebutton6 = (ImageButton) findViewById(R.id.imageButton6); 

       imagebutton6.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View arg0) { 

         Intent intent = new Intent(context, Web.class); 
            startActivity(intent); 

        } 

       }); 
    } 
} 

**

MainActivityWebview.java 

import android.app.Activity; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 

    public class MainActivityWebView extends Activity { 

     private Button button1, button2; 

     public void onCreate(Bundle savedInstanceState) { 
      final Context context = this; 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.fashion); 

      button1 = (Button) findViewById(R.id.button1); 

      button1.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
       Intent intent = new Intent(context, WebViewActivitySelfridges.class); 
       startActivity(intent); 
       } 

      }); 
      button2 = (Button) findViewById(R.id.button2); 

      button2.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
       Intent intent = new Intent(context, WebViewActivityVery.class); 
       startActivity(intent); 
       } 

      }); 


     } 

    } 

fashion.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FFFFFF" 

     > 
    <ScrollView 
      android:id="@+id/tabscrvie" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      > 
      <TableLayout 
       android:id="@+id/gridview" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       > 

      <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView1" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:src="@drawable/selfridges" /> 

       <Button 
        android:id="@+id/button1" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Store" /> 

    </TableRow> 

      <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView2" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:src="@drawable/very" /> 

       <Button 
        android:id="@+id/button2" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

      <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView3" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:src="@drawable/nelly" /> 

       <Button 
        android:id="@+id/button3" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

      <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView4" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="20dp" 
        android:layout_weight="1" 
        android:baselineAlignBottom="false" 
        android:cropToPadding="true" 
        android:src="@drawable/george" /> 

       <Button 
        android:id="@+id/button4" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView5" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="20dp" 
        android:layout_weight="1" 
        android:baselineAlignBottom="false" 
        android:cropToPadding="true" 
        android:src="@drawable/redoute" /> 

       <Button 
        android:id="@+id/button5" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView6" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="20dp" 
        android:layout_weight="1" 
        android:baselineAlignBottom="false" 
        android:cropToPadding="true" 
        android:src="@drawable/fraser" /> 

       <Button 
        android:id="@+id/button6" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView7" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="20dp" 
        android:layout_weight="1" 
        android:baselineAlignBottom="false" 
        android:cropToPadding="true" 
        android:src="@drawable/lncc" /> 

       <Button 
        android:id="@+id/button7" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView1" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:src="@drawable/selfridges" /> 

       <Button 
        android:id="@+id/button1" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Store" /> 

    </TableRow> 

      <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView2" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:src="@drawable/very" /> 

       <Button 
        android:id="@+id/button2" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

      <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView3" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:src="@drawable/nelly" /> 

       <Button 
        android:id="@+id/button3" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

      <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView4" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="20dp" 
        android:layout_weight="1" 
        android:baselineAlignBottom="false" 
        android:cropToPadding="true" 
        android:src="@drawable/george" /> 

       <Button 
        android:id="@+id/button4" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView5" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="20dp" 
        android:layout_weight="1" 
        android:baselineAlignBottom="false" 
        android:cropToPadding="true" 
        android:src="@drawable/redoute" /> 

       <Button 
        android:id="@+id/button5" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView6" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="20dp" 
        android:layout_weight="1" 
        android:baselineAlignBottom="false" 
        android:cropToPadding="true" 
        android:src="@drawable/fraser" /> 

       <Button 
        android:id="@+id/button6" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/imageView7" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="20dp" 
        android:layout_weight="1" 
        android:baselineAlignBottom="false" 
        android:cropToPadding="true" 
        android:src="@drawable/lncc" /> 

       <Button 
        android:id="@+id/button7" 
        android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="20dp" 
      android:layout_weight="1" 
        android:text="Visit Website" /> 

    </TableRow> 



    </TableLayout> 
    </ScrollView> 
    </RelativeLayout> 

WebViewActivitySelfridges.java

import android.app.Activity; 
    import android.os.Bundle; 
    import android.webkit.WebView; 

    public class WebViewActivitySelfridges 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.selfridges.com/"); 

     } 

    } 

WebViewActivityVery.java

import android.app.Activity; 
    import android.os.Bundle; 
    import android.webkit.WebView; 

    public class WebViewActivityVery 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.very.co.uk/"); 

     } 

    } 

webview.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" 
/> 

清单

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="store.front" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     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="store.front.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:label="@string/app_name" 
      android:name="store.front.WebViewActivitySelfridges" > 
     </activity> 

     <activity 
      android:label="@string/app_name" 
      android:name="store.front.WebViewActivityVery" > 
     </activity> 

     <activity 
      android:label="@string/app_name" 
      android:name="store.front.Fashion" > 
     </activity> 

     <activity 
      android:label="@string/app_name" 
      android:name="store.front.Electrical" > 
     </activity> 

     <activity 
      android:label="@string/app_name" 
      android:name="store.front.Kids" > 
     </activity> 

     <activity 
      android:label="@string/app_name" 
      android:name="store.front.Food" > 
     </activity> 

     <activity 
      android:label="@string/app_name" 
      android:name="store.front.Pets" > 
     </activity> 

     <activity 
      android:label="@string/app_name" 
      android:name="store.front.Web" > 
     </activity> 

     <activity 
      android:label="@string/app_name" 
      android:name="store.front.MainActivityWebView" > 
     </activity> 



    </application> 

</manifest> 
+0

您是否将INTERNET权限添加到清单中? – hatcyl

+0

是的,我现在添加了我的清单文件。 – TheWelshOne

回答

0

不能完全肯定,如果这是正确的。

@Override在onCreate中缺少您定义的所有webview活动。

检查onCreate()是否被触发。

+0

不幸的是没有运气。 – TheWelshOne