0

所以我有一个微调器,它需要使用位于 assets 文件夹中的 HTML 文件来填充 WebView。我的代码看起来很血统,但显然不起作用。我将附上Java和错误。

我知道我在这里犯了错误,需要了解我做错了什么。

public class atcSectionWeb extends Activity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

Spinner spLoadFrom;
private ArrayAdapter<CharSequence> spinnerArrayAdapter;

String name[] = { "1.html", "2.html", "3.html", "etc.html" };
String displayName[] = {"1st Name", "2nd Name", "3rd name", "And So On" };

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.atcsectionweb);

    mProgressDialog = new ProgressDialog(atcSectionWeb.this);
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setMax(100);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    spLoadFrom = (Spinner) findViewById(R.id.Spinner02);

    spinnerArrayAdapter = new ArrayAdapter<CharSequence>(this,
            android.R.layout.simple_spinner_item, displayName);
    spinnerArrayAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spLoadFrom.setAdapter(spinnerArrayAdapter);

    SpinnerListener spListener = new SpinnerListener();
    spLoadFrom.setOnItemSelectedListener(spListener);


    Button atcBack = (Button) findViewById(R.id.atcBacksecweb);
    atcBack.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            setResult(RESULT_OK);
            finish();
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.atcAbout2:
        Intent atcAboutWeb = new Intent(atcSectionWeb.this,
                atcAboutWeb.class);
        startActivity(atcAboutWeb);
        break;
    case R.id.atcContact2:
        emailme();
        break;
    }
    return true;
}

private void emailme() {
    // TODO Auto-generated method stub
    try{
    String domsEmail = "";
    String message = "Insert Message Here";
    String myemail[] = { domsEmail };
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, myemail);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    startActivity(emailIntent);
    }catch (Exception  e) {
        Toast.makeText(
                atcSectionWeb.this,
                "No email application is available, please download one from Play store.",
                Toast.LENGTH_LONG).show();

    }
}

public class SpinnerListener implements OnItemSelectedListener {
    public SpinnerListener() {
    }

    public void onItemSelected(AdapterView<?> arg0, View arg1,
            final int position, long arg2) {

        Button atcSection = (Button) findViewById(R.id.atcSubmitweb);
        atcSection.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                WebView wv = (WebView)findViewById(R.id.ctiWebView);
                wv.setWebViewClient(new WebViewClient() {  
                      @Override  
                      public boolean shouldOverrideUrlLoading(WebView view, String url)  
                      {  
                        view.loadUrl(url);
                        return true;
                      }  
                    });         
                wv.loadUrl("file:///android_asset/" + position); 




            }
        });

    }

    public void onNothingSelected(AdapterView<?> arg0) {

    }
}
}

日志猫:

07-28 23:40:09.438: E/AndroidRuntime(584): FATAL EXCEPTION: main
07-28 23:40:09.438: E/AndroidRuntime(584): java.lang.NullPointerException
07-28 23:40:09.438: E/AndroidRuntime(584):  at com.assistant.atcSectionWeb$SpinnerListener$1.onClick(atcSectionWeb.java:240)
07-28 23:40:09.438: E/AndroidRuntime(584):  at android.view.View.performClick(View.java:2485)
07-28 23:40:09.438: E/AndroidRuntime(584):  at android.view.View$PerformClick.run(View.java:9080)
07-28 23:40:09.438: E/AndroidRuntime(584):  at android.os.Handler.handleCallback(Handler.java:587)
07-28 23:40:09.438: E/AndroidRuntime(584):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-28 23:40:09.438: E/AndroidRuntime(584):  at android.os.Looper.loop(Looper.java:123)
07-28 23:40:09.438: E/AndroidRuntime(584):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-28 23:40:09.438: E/AndroidRuntime(584):  at java.lang.reflect.Method.invokeNative(Native Method)
07-28 23:40:09.438: E/AndroidRuntime(584):  at java.lang.reflect.Method.invoke(Method.java:507)
07-28 23:40:09.438: E/AndroidRuntime(584):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-28 23:40:09.438: E/AndroidRuntime(584):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-28 23:40:09.438: E/AndroidRuntime(584):  at dalvik.system.NativeStart.main(Native Method)

复制 SALMAN 的代码后出错:

07-29 01:04:58.290: E/AndroidRuntime(532): FATAL EXCEPTION: main
07-29 01:04:58.290: E/AndroidRuntime(532): java.lang.NullPointerException
07-29 01:04:58.290: E/AndroidRuntime(532):  at com.assistant.atcSectionWeb$SpinnerListener.onItemSelected(atcSectionWeb.java:236)
07-29 01:04:58.290: E/AndroidRuntime(532):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
07-29 01:04:58.290: E/AndroidRuntime(532):  at android.widget.AdapterView.access$200(AdapterView.java:42)
07-29 01:04:58.290: E/AndroidRuntime(532):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
07-29 01:04:58.290: E/AndroidRuntime(532):  at android.os.Handler.handleCallback(Handler.java:587)
07-29 01:04:58.290: E/AndroidRuntime(532):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 01:04:58.290: E/AndroidRuntime(532):  at android.os.Looper.loop(Looper.java:123)
07-29 01:04:58.290: E/AndroidRuntime(532):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-29 01:04:58.290: E/AndroidRuntime(532):  at java.lang.reflect.Method.invokeNative(Native Method)
07-29 01:04:58.290: E/AndroidRuntime(532):  at java.lang.reflect.Method.invoke(Method.java:507)
07-29 01:04:58.290: E/AndroidRuntime(532):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-29 01:04:58.290: E/AndroidRuntime(532):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-29 01:04:58.290: E/AndroidRuntime(532):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

粘贴应用程序的整个代码只是为了您的目的。

 package asdasd.adasd.ad;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class AsdasdasActivity extends Activity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

Spinner spLoadFrom;
private ArrayAdapter<CharSequence> spinnerArrayAdapter;

String name[] = { "1.html", "2.html", "3.html"};
String displayName[] = {"1st Name", "2nd Name", "3rd name" };

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mProgressDialog = new ProgressDialog(AsdasdasActivity.this);
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setMax(100);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    spLoadFrom = (Spinner) findViewById(R.id.Spinner02);

    spinnerArrayAdapter = new ArrayAdapter<CharSequence>(this,
            android.R.layout.simple_spinner_item, displayName);
    spinnerArrayAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spLoadFrom.setAdapter(spinnerArrayAdapter);

    SpinnerListener spListener = new SpinnerListener();
    spLoadFrom.setOnItemSelectedListener(spListener);


    WebView wv = (WebView)findViewById(R.id.webView1);

    wv.getSettings().setJavaScriptEnabled(true);
 /*   wv.setWebViewClient(new WebViewClient() {  
          @Override  
          public boolean shouldOverrideUrlLoading(WebView view, String url)  
          {  
            view.loadUrl(url);
            return true;
          }  
        });    
        */     
    wv.loadUrl("file:///android_asset/htmlfileMine.html"); 


    Button atcBack = (Button) findViewById(R.id.atcBacksecweb);
    atcBack.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            setResult(RESULT_OK);
            finish();
        }
    });

}



private void emailme() {
    // TODO Auto-generated method stub
    try{
    String domsEmail = "";
    String message = "Insert Message Here";
    String myemail[] = { domsEmail };
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, myemail);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    startActivity(emailIntent);
    }catch (Exception  e) {
        Toast.makeText(
                AsdasdasActivity.this,
                "No email application is available, please download one from Play store.",
                Toast.LENGTH_LONG).show();

    }
}

public class SpinnerListener implements OnItemSelectedListener {
    public SpinnerListener() {
    }

    public void onItemSelected(AdapterView<?> arg0, View arg1,
            final int position, long arg2) {


                WebView wv = (WebView)findViewById(R.id.webView1);

                wv.getSettings().setJavaScriptEnabled(true);
               wv.setWebViewClient(new WebViewClient() {  
                      @Override  
                      public boolean shouldOverrideUrlLoading(WebView view, String url)  
                      {  
                        view.loadUrl(url);
                        return true;
                      }  
                    });    

                wv.loadUrl("file:///android_asset/"+name[position]); 






    }

    public void onNothingSelected(AdapterView<?> arg0) {

    }
}
}

/// XML 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Spinner
        android:id="@+id/Spinner02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/atcSubmitweb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/atcBacksecweb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <WebView
        android:id="@+id/webView1"
        android:layout_width="300dp"
        android:layout_height="300dp" />

</LinearLayout>

谢谢。

于 2012-07-29T00:41:49.793 回答
1
//call that class on button click evevnt
WebDialog dialog =new WebDialog(this,"file:///android_asset/index.html");
        dialog.show();

//Put this class into your package
public class WebDialog extends Dialog
{

    static final int                      BLUE                  = 0xFF6D84B4;
    static final float[]                  DIMENSIONS_DIFF_LANDSCAPE =
                                                                    { 20, 60 };
    static final float[]                  DIMENSIONS_DIFF_PORTRAIT  =
                                                                    { 40, 60 };
    static final FrameLayout.LayoutParams   FILL                    = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    static final int                      MARGIN                    = 4;
    static final int                      PADDING                   = 2;
    static final String                   DISPLAY_STRING            = "touch";

    private String                        mUrl;
//  private DialogListener                mListener;
    private ProgressDialog                mSpinner;
    private WebView                       mWebView;
    private LinearLayout                  mContent;
    private TextView                      mTitle;

    public WebDialog(Context context, String url)
    {
        super(context);
        mUrl = url;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mSpinner = new ProgressDialog(getContext());
        mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mSpinner.setMessage("Loading...");

        mContent = new LinearLayout(getContext());
        mContent.setOrientation(LinearLayout.VERTICAL);
        setUpTitle();
        setUpWebView();
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        final float scale = getContext().getResources().getDisplayMetrics().density;
        int orientation = getContext().getResources().getConfiguration().orientation;
        float[] dimensions = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
        addContentView(mContent, new LinearLayout.LayoutParams(display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)), display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
    }

    private void setUpTitle()
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Drawable icon = getContext().getResources().getDrawable(R.drawable.ic_launcher);
        mTitle = new TextView(getContext());
        mTitle.setText("Website");
        mTitle.setTextColor(Color.WHITE);
        mTitle.setTypeface(Typeface.DEFAULT_BOLD);
        mTitle.setBackgroundColor(BLUE);
        mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
//      mTitle.setCompoundDrawablePadding(MARGIN + PADDING);
//      mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
        mContent.addView(mTitle);
    }

    private void setUpWebView()
    {
        mWebView = new WebView(getContext());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new WebDialog.DialogWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);

        System.out.println(" mURL = "+mUrl);

        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(FILL);
        mContent.addView(mWebView);
    }

    private class DialogWebViewClient extends WebViewClient
    {

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

            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            super.onReceivedError(view, errorCode, description, failingUrl);
            WebDialog.this.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon)
        {
            super.onPageStarted(view, url, favicon);
            mSpinner.show();
        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            super.onPageFinished(view, url);
            String title = mWebView.getTitle();
            if (title != null && title.length() > 0)
            {
                mTitle.setText(title);
            }
            mSpinner.dismiss();
        }

    }
}
于 2012-07-30T11:24:02.053 回答