概述
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.
I know I'm making mistakes here and need to learn what I am doing wrong.
public class atcSectionWeb extends Activity {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
Spinner spLoadFrom;
private ArrayAdapter 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(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) {
}
}
}
LogCat:
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)
Error after copying SALMAN's 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)
最后
以上就是玩命春天为你收集整理的安卓 spinner html,html - Android - Load WebView from Asset using a Spinner - Stack Overflow的全部内容,希望文章能够帮你解决安卓 spinner html,html - Android - Load WebView from Asset using a Spinner - Stack Overflow所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复