为什么不正确运行我的程序?

时间:2012-10-22 08:36:52

标签: android webview

我想创建一个页面底部是5个按钮的页面,页面的其余部分用于当用户按下其中一个按钮时。我创建了这个但是当我按下Home按钮时,程序什么都不做,而且没有打开html文件。为什么呢?

RES /布局/ activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <WebView
        android:id="@+id/web_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0" />
    <LinearLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:background="@color/background"  
        android:gravity="bottom">
    <Button
        android:id="@+id/button_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_home"
        android:textColor="@color/text_home"
        android:drawableTop="@drawable/home_icon"
        style="?android:attr/borderlessButtonStyle"/>
    <Button 
        android:id="@+id/button_product"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_product"
        android:textColor="@color/text_product"
        android:drawableTop="@drawable/product_icon"
        android:onClick="Product"
        style="?android:attr/borderlessButtonStyle"/>
    <Button
        android:id="@+id/button_places"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_places"
        android:textColor="@color/text_places" 
        android:drawableTop="@drawable/places_icon"
        android:onClick="Places"
        style="?android:attr/borderlessButtonStyle"/>
    <Button 
        android:id="@+id/button_rewards"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_rewards"
        android:textColor="@color/text_rewards"
        android:drawableTop="@drawable/rewards_icon"
        android:onClick="Rewards"
        style="?android:attr/borderlessButtonStyle"/>
    <Button 
        android:id="@+id/button_more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_more"
        android:textColor="@color/text_more"
        android:drawableTop="@drawable/more_icon"
        android:onClick="More"
        style="?android:attr/borderlessButtonStyle"/>

    </LinearLayout>
</LinearLayout>

RES /值/ colors.xml

<resources>
    <color name="background">#000</color>
    <color name="text_home">#888</color>
    <color name="text_product">#888</color>
    <color name="text_places">#888</color>
    <color name="text_rewards">#888</color>
    <color name="text_more">#888</color>
</resources>

RES /值/ 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="button_home">Home</string>
    <string name="button_product">Product</string>
    <string name="button_places">Places</string>
    <string name="button_rewards">Rewards</string>
    <string name="button_more">More</string>
</resources>

资产/ index.html中

<html>
    <body bgcolor="yellow">
        <center>
            <h2>Hello W3Schools!</h2>
        </center>
    </body>
</html>

home.java

package org.example.test;

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

public class home extends Activity{
private WebView webView;
private Button home;
@Override 
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView=(WebView)findViewById(R.id.web_view);
    home=(Button)findViewById(R.id.button_home);
    //
    home.setOnClickListener(new OnClickListener(){
        public void onClick(View view){
            webView.loadUrl("file:///android_asset/index.html");
        }
    });
}

0 个答案:

没有答案