单击按钮时Webview不会加载?

时间:2013-07-18 16:54:22

标签: android android-activity webview

我一直试图解决这个问题已有好几天了,我已经在网上搜索过,并且已经浏览了这里提供的许多相关问题,但我还没有解决这个问题。

当我创建一个单独的项目时代码工作正常,但有些原因它在这里不起作用。

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

我已将所有活动添加到Manifest(我能看到的)多次查看过。大声笑

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

感谢您的帮助。

** 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>

1 个答案:

答案 0 :(得分:0)

不完全确定这是否正确。

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

检查onCreate()是否完全被触发。