所以我有一个微调器,它需要使用位于 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)