2016-03-07 51 views
0

我做了我的第一個android應用程序,我想只顯示我的應用程序中的小菜單的網頁。但菜單是我的大問題。 這是我的代碼在我的content_main.xml中,繪製我的菜單。Android菜單 - 項目不活躍

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="16dp"> 

    <io.github.yavski.fabspeeddial.FabSpeedDial 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|start" 
     app:fabGravity="bottom_start" 
     app:fabMenu="@menu/menu_main" 
     app:miniFabBackgroundTint="@android:color/holo_blue_bright" 
     app:miniFabDrawableTint="?attr/colorPrimary" 
     app:miniFabTitleTextColor="?attr/colorPrimaryDark" /> 

</FrameLayout> 

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context=".MainActivity"> 

<item 
    android:id="@+id/action_refresh" 
    android:icon="@drawable/ic_refresh_white_24px" 
    android:title="@string/menu_item_refresh" 
    android:onClick="refresh" 
/> 
<item 
    android:id="@+id/action_share" 
    android:icon="@drawable/ic_share_white_24px" 
    android:title="@string/menu_item_share" 
    android:onClick="refresh"/> 
<item 
    android:id="@+id/action_about" 
    android:icon="@drawable/ic_about_white_24px" 
    android:title="@string/menu_item_about" 
    android:onClick="refresh"/> 
<item 
    android:id="@+id/action_exit" 
    android:icon="@drawable/ic_exit_white_24px" 
    android:title="@string/menu_item_exit" 
    android:onClick="refresh"/> 
</menu> 

MainActivity.java:

package com.example.roman.projectkosican; 

import android.app.Activity; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 

import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 

public class MainActivity extends AppCompatActivity { 

/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 

final Activity activity = this; 
private WebView webView = null; 
/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 

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


    webView = (WebView) findViewById(R.id.webView); 
    webView.getSettings().setJavaScriptEnabled(true); 


    webView.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 
      activity.setTitle("Loading..."); 
      activity.setProgress(progress * 100); 

      if (progress == 100) 
       activity.setTitle(R.string.app_name); 
     } 
    }); 

    webView.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onReceivedError(WebView view, int errorCode, String   description, String failingUrl) { 
      // Handle the error 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    }); 

    webView.loadUrl("http://dniobce.sk"); 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 

} 

@Override 
public void onStart() { 
    super.onStart(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client.connect(); 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Main Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app deep link URI is correct. 
      Uri.parse("android-app://com.example.roman.projectkosican/http/host/path") 
    ); 
    AppIndex.AppIndexApi.start(client, viewAction); 
} 

@Override 
public void onStop() { 
    super.onStop(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Main Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app deep link URI is correct. 
      Uri.parse("android-app://com.example.roman.projectkosican/http/host/path") 
    ); 
    AppIndex.AppIndexApi.end(client, viewAction); 
    client.disconnect(); 
} 

public void refresh(View v) { 
    webView.loadUrl("http://www.google.com"); 
    webView.reload(); 
} 

,當我在圖標它調用刷新方法單擊,沒什麼happends

+0

您可以分享片段/活動代碼嗎? –

+0

你的意思是MainActivity.java? – Roman

+0

是的。 MainActivity.java –

回答

0

該方法需要採取查看例如:

public void refresh(View v) { 
    webView.loadUrl("http://www.google.com"); 
    webView.reload(); 
} 
+0

仍然無法正常工作。 – Roman

+0

通過將其打印到控制檯或顯示吐司來確保該方法正在運行。我不認爲你需要加載一個url後重新加載webView。 – MDubzem

+0

是的,這是真的,但它並沒有解決我的問題。 – Roman

0

你必須改變你的refresh()方法是這樣的:

public void refresh(MenuItem menuItem){ 
     webView.loadUrl("http://www.google.com"); 
     webView.reload(); 
    } 

應該接受MenuItem作爲參數,因爲您要調用從菜單中選擇該方法。

+0

不,它沒有幫助。 – Roman