Android意图无法在操作栏中工作

时间:2016-07-09 16:30:07

标签: java android xml android-intent

我必须尝试制作基于webview的应用程序并且我已经编写了一些代码,但是例如设置按钮调用函数和函数调用设置活动但是设置活动将不会调用,无论如何例如在浏览器按钮调用中打开浏览器有意图然后它工作,我的代码有什么问题?

Cizgiroman.java

    package com.tirtak.cizgihaberapp;

import java.util.ArrayList;

import com.tirtak.cizgihaberapp.Database;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebSettings.TextSize;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;


public class Cizgiroman extends Activity {

    private WebView webview;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_cizgiroman);

        SharedPreferences ayarlariCek = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
        webview = (WebView) findViewById(R.id.webView1);
        boolean jsAbility = ayarlariCek.getBoolean("js", true);
          webview.getSettings().setJavaScriptEnabled(jsAbility);
          if(getIntent().getExtras() != null){
              Intent intent = getIntent();
                String action = intent.getAction();
                    Uri data = intent.getData();
                    url = data.toString();
                webview.loadUrl(url);
                }else {webview.loadUrl(getString(R.string.CizgiromanURL));}
          webview.setWebViewClient(new WebViewClient());
        final Activity MyActivity = this;
          webview.setWebChromeClient(new WebChromeClient(){
              public void onProgressChanged(WebView view, int progress){
                  //MyActivity.setTitle("Yükleniyor...");
                  MyActivity.setProgress(progress * 100);

                  if(progress == 100){}
              }
          });
          webview.getSettings().setRenderPriority(RenderPriority.HIGH);
          webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
          webview.getSettings().setUserAgentString("Cizgi Haber");
          int txtSize = ayarlariCek.getInt("txtSize", 3);
          switch (txtSize){
          case 1:
              webview.getSettings().setTextSize(TextSize.SMALLEST);
              break;
          case 2:
              webview.getSettings().setTextSize(TextSize.SMALLER);
              break;
          case 3:
              webview.getSettings().setTextSize(TextSize.NORMAL);
              break;
          case 4:
              webview.getSettings().setTextSize(TextSize.LARGER);
              break;
          case 5:
              webview.getSettings().setTextSize(TextSize.LARGEST);
              break;
          }
          boolean fileReach = ayarlariCek.getBoolean("fileReach", false);
          webview.getSettings().setAllowFileAccess(fileReach);
          boolean zoomAbility = ayarlariCek.getBoolean("zoom", false);
          webview.getSettings().setSupportZoom(zoomAbility);
          boolean gpsAbility = ayarlariCek.getBoolean("gps", false);
          webview.getSettings().setGeolocationEnabled(gpsAbility);
          boolean windowsJsAbility = ayarlariCek.getBoolean("windowJs", false);
          webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(windowsJsAbility);
          boolean opAbility = ayarlariCek.getBoolean("picture", true);
          webview.getSettings().setLoadsImagesAutomatically(opAbility);
          boolean sPAbility = ayarlariCek.getBoolean("password", false);
          webview.getSettings().setSaveFormData(sPAbility);
          webview.getSettings().setSavePassword(sPAbility);
          boolean dSAbility = ayarlariCek.getBoolean("desktop", false);
          if(dSAbility){
              webview.getSettings().setLoadWithOverviewMode(true);
          }

    }

    @Override
    public void onBackPressed() {
        if(webview.canGoBack()){
            webview.goBack();
        }else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionmenu, menu);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true); 
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings){
            openSettings();
    }else if (id == R.id.action_arama){
            search();
    }else if (id == R.id.action_reload){
            reload();
    }else if (id == R.id.action_openBrowser){
            openBrowser();
    }else if (id == R.id.action_share){
            share();
    }else if (id == R.id.action_savepage){
            savepage();
    }
        return true;
    }

    public void openSettings() {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }

    public void search() {
        SharedPreferences ayarlariCek = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        int searchMode = ayarlariCek.getInt("searchMod", 1);
            Intent i = new Intent(this, AppSearch.class);
            i.putExtra("site", R.string.CizgiromanURL);
            startActivity(i);
    }

    public void savepage() {
        /*SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = preferences.edit();
        int urlcounter = preferences.getInt("urlcounter", 0);
        //editor.commit();
        String urlcountname;
        if(urlcounter == 0){
            urlcountname = "url0";
            editor.putString(urlcountname, webview.getUrl());
            editor.putInt("urlcounter" , 1);
            editor.commit();
        }else {
            int newurlcount = urlcounter++;
            urlcountname = "url" + newurlcount;
            editor.putString(urlcountname, webview.getUrl());
            editor.putInt("urlcounter", newurlcount);
            editor.commit();
        }*/
        String page_head = webview.getTitle();
        String page_url = webview.getUrl();
        String page_save_date = "N/A";
        String page_host = "cizgiroman";
        Database db = new Database(getApplicationContext());
        db.kitapEkle(page_head, page_url, page_save_date, page_host);//kitap ekledik
        db.close();
        Toast.makeText(getApplicationContext(), "Sayfa Başarıyla Kaydedildi.", Toast.LENGTH_LONG).show();
    }

    public void reload() {
        webview.reload();
    }

    public void openBrowser() {
        //webview.getUrl();
        Uri uri = Uri.parse(webview.getUrl()); // missing 'http://' will cause crashed
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    public void share() {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        ArrayList<String> content = new ArrayList<String>();
        content.add(webview.getTitle());
        content.add(webview.getUrl());
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, webview.getUrl() + "    " + webview.getTitle() + "    (Çizgi Haber ile Paylaşıldı)");
        startActivity(sharingIntent.createChooser(sharingIntent, "Paylaşmak İçin Seçiniz"));
    }
}

actionbar.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.tirtak.cizgihaberapp.Cizgiroman" >
    <item
        android:id="@+id/action_search"
        android:showAsAction="ifRoom"
        android:icon="@drawable/ic_search"
        android:title="@string/action_arama" />
    <item
        android:id="@+id/action_savepage"
        android:showAsAction="ifRoom"
        android:icon="@drawable/ic_bookmark"
        android:title="@string/action_savedpages" />
    <item
        android:id="@+id/action_share"
        android:showAsAction="ifRoom"
        android:icon="@drawable/ic_share"
        android:title="@string/action_share"/>
    <item
        android:id="@+id/action_reload"
        android:showAsAction="never"
        android:icon="@drawable/ic_autorenew"
        android:title="@string/action_reload"/>
    <item
        android:id="@+id/action_openBrowser"
        android:showAsAction="never"
        android:icon="@drawable/ic_open_in_browser"
        android:title="@string/action_browser"/>
    <item
        android:id="@+id/action_setting"
        android:showAsAction="never"
        android:icon="@drawable/ic_settings"
        android:title="@string/action_settings"/>

</menu>

activity_cizgiroman.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tirtak.cizgihaberapp.Cizgiroman" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

0 个答案:

没有答案