让webview加载微调器XML值

时间:2015-12-03 15:29:31

标签: java android

我正在尝试在Android Studio中创建一个应用程序,您可以在其中定义微调器中的URL。当您在微调器中选择一个项目时,它应该将URL加载到微调器中。

我已经得到了一些帮助,但现在我面临另一个问题:我得到了:

  

无法解析符号方法loadUrl java.lang.strin

我的XML代码:

<resources>
<string name="app_name">Obligatorisk1</string>
   <string-array name="spinner">
    <item value="http://www.vg.no">VG</item>
    <item value="http://www.dagbladet.no">Dagbladet</item>
    <item value="http://www.nettavisen.no">Nettavisen</item>
</string-array>
<string name="omAppen">Om Appen</string>
<string name="title_activity_main2">Om Appen</string>

我的主要活动:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;




public class MainActivity extends AppCompatActivity implements 
AdapterView.OnItemSelectedListener{

private Button omAppenKnapp;
Spinner spinner;
WebView wb1;

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

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


    ArrayAdapter adapter=ArrayAdapter.createFromResource(this, 
R.array.spinner, android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new 
AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, 
int i, long l) {

            String urlName = adapterView.getItemAtPosition(i).toString();

            String urlValue = "http://www.google.com";

            switch (urlName) {
                case "VG":
                    urlValue = "http://www.vg.no";
                    break;

                case "Dagbladet":
                    urlValue = "http://www.dagbladet.no";
                    break;

                case "Nettavisen":
                    urlValue = "http://www.nettavisen.no";
                    break;
            }
            view.loadUrl(urlValue);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            wb1.loadUrl("http://www.google.com");
        }
    });
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,  
long id) {

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}

和布局:

<?xml version="1.0" encoding="utf-8"?>
<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" 
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity">

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webView"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/spinner" />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/omAppen"
    android:id="@+id/buttonOmOss"
    android:onClick="visOmAppen"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

试试这个:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String selected_url = spinner.getSelectedItem().toString();
    view.loadUrl(selected_url);
}

修改

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

private Button omAppenKnapp;
Spinner spinner;
WebView view;

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

    spinner= (Spinner) findViewById(R.id.spinner);
    ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.spinner, android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

    view = (WebView) findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new browser());
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String selected_url = spinner.getSelectedItem().toString();
    view.loadUrl(selected_url);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

答案 1 :(得分:0)

您可以这样做:

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    WebView webView;

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

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


        ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.spinner, android.R.layout.simple_spinner_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                String urlName = adapterView.getItemAtPosition(i).toString();

                String urlValue = "http://www.google.com";

                switch (urlName)
                {
                    case "VG":
                        urlValue ="http://www.vg.no";
                        break;

                    case "Dagbladet":
                        urlValue = "http://www.dagbladet.no";
                        break;

                    case "Nettavisen":
                        urlValue = "http://www.nettavisen.no";
                        break;
                }
                webView.loadUrl(urlValue);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                webView.loadUrl("http://www.google.com");
            }
        });
    }
}