Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Load WebView from Asset using a Spinner
    primarykey
    data
    text
    <p>So I have a spinner that needs to populate a WebView using an HTML file located in the assets folder. I have my code that looks descent but obviously doesn't work. I will attach the Java and the error. </p> <p>I know I'm making mistakes here and need to learn what I am doing wrong.</p> <pre><code>public class atcSectionWeb extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private ProgressDialog mProgressDialog; Spinner spLoadFrom; private ArrayAdapter&lt;CharSequence&gt; 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&lt;CharSequence&gt;(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&lt;?&gt; 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&lt;?&gt; arg0) { } } } </code></pre> <p>LogCat:</p> <pre><code>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) </code></pre> <p>Error after copying SALMAN's code:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload