我有一个使用 Sherlock 操作栏的应用程序,我已经拆分了操作栏。单击操作栏中的下一个项目时,我正在尝试加载下一个 html 文件。但它告诉我错误。这是我的代码。
活动.java:
public class TutorialViewActivity extends SherlockActivity {
// Tutorial No
int TutorialNo;
// progress dialog
ProgressDialog mProgress;
// Share String
String copy;
// String for URL
String URL;
// Settings Values - COMPLETED
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check Settings
settings();
// Check Full Screen
if (Fullscreen_Sett == true) {
// hide statusbar of Android
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// orientation Settings
if (Orientation_Sett.equals("Portrait")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (Orientation_Sett.equals("Landscape")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
// showing layout
setContentView(R.layout.tutorialview_layout);
// Backlight ON/OFF
if (Backlight_Sett == true) {
getWindow()
.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
// Getting Strings(level) From Main Activity
Bundle extras = getIntent().getExtras();
String TutorialName = extras.getString("TutorialName");
TutorialNo = extras.getInt("TutorialNo");
TutorialNo++;
// Loading file html link
URL = "file:///android_asset/Tutorials/" + TutorialNo
+ "/androidtutorial.html";
String copy_share = "tutorials" + TutorialNo + "/copyshare.txt";
// Getting Text for copy share
try {
copy = readTxt(copy_share);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// ActionBar
ActionBar ActionBar = getSupportActionBar();
ActionBar.setTitle("CHAPTER " + " : " + TutorialNo);
ActionBar.setSubtitle(TutorialName);
ActionBar.setDisplayHomeAsUpEnabled(true);
ActionBar.setHomeButtonEnabled(true);
// method called for tutorialview
tutorialview();
}
/************************************ SETTINGS ******************/
public void settings() {
// SETTINGS
SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(this);
Zoom_Sett = SP.getBoolean("zoomcontrols_settings", true);
fitscreen_settings = SP.getBoolean("fitscreen_settings", false);
Animation_Sett = SP.getBoolean("animations_settings", true);
Fullscreen_Sett = SP.getBoolean("fullscreen_settings", false);
Backlight_Sett = SP.getBoolean("backlight_settings", false);
Orientation_Sett = SP.getString("orientation_settings", "Sensor");
}
// Method for tutorial view
public void tutorialview() {
// webview
WebView wv = (WebView) findViewById(R.id.TutorialWebView);
// webview settings
WebSettings webSettings = wv.getSettings();
// if (Orientation_Sett.equals("Portrait")) {
// new webview
wv = new WebView(this);
// contentview of webview for progressdialog
setContentView(wv);
// the state of progress dialog
mProgress = ProgressDialog.show(this, "Loading",
"Please wait for a moment...");
// add a WebViewClient for WebView, which actually handles loading
// data
// from web
wv.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if (mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
// }
// Webview controls
if (Zoom_Sett == true) {
webSettings.setBuiltInZoomControls(true);
wv.loadUrl(URL);
} else if (fitscreen_settings == true) {
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
wv.loadUrl(URL);
} else {
wv.loadUrl(URL);
}
}
/****************************************************************************/
// Dropdown Menu
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.tutorialview, menu);
return super.onCreateOptionsMenu(menu);
}
/****************************************************************************/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
// Home Back ActionBAr
case android.R.id.home:
onBackPressed();
break;
case R.id.copy:
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(copy);
Toast.makeText(getApplicationContext(), "Coped To Clipboard",
Toast.LENGTH_SHORT).show();
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData
.newPlainText("", copy);
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "Coped To Clipboard",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.next:
TutorialNo++;
// Loading file html link
URL = "file:///android_asset/Tutorials/" + TutorialNo
+ "/androidtutorial.html";
// method called for tutorialview
//tutorialview();
WebView wv = (WebView) findViewById(R.id.TutorialWebView);
wv.loadUrl(URL);
Toast.makeText(getApplicationContext(), "Next", Toast.LENGTH_SHORT).show();
}
return true;
}// Home Back ActionBAr End
// Reading TextFiles
public String readTxt(String copy_share) throws IOException {
InputStream inputStream = getAssets().open(copy_share);
// System.out.println(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
// Go Back
@Override
public void onBackPressed() {
super.finish();
if (Animation_Sett == true) {
// Fading Transition Effect
TutorialViewActivity.this.overridePendingTransition(
android.R.anim.fade_in, android.R.anim.fade_out);
}
}// END GO BACK
}
日志错误:
08-08 12:53:18.986: E/AndroidRuntime(4312): FATAL EXCEPTION: main
08-08 12:53:18.986: E/AndroidRuntime(4312): java.lang.NullPointerException
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.danaraddi.androidtutorial.TutorialViewActivity.onMenuItemSelected(TutorialViewActivity.java:233)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:604)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchOptionsItemSelected(ActionBarSherlockNative.java:92)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.actionbarsherlock.app.SherlockActivity.onOptionsItemSelected(SherlockActivity.java:159)
08-08 12:53:18.986: E/AndroidRuntime(4312): at android.app.Activity.onMenuItemSelected(Activity.java:2548)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
08-08 12:53:18.986: E/AndroidRuntime(4312): at android.view.View.performClick(View.java:4202)
08-08 12:53:18.986: E/AndroidRuntime(4312): at android.view.View$PerformClick.run(View.java:17340)
08-08 12:53:18.986: E/AndroidRuntime(4312): at android.os.Handler.handleCallback(Handler.java:725)
08-08 12:53:18.986: E/AndroidRuntime(4312): at android.os.Handler.dispatchMessage(Handler.java:92)
08-08 12:53:18.986: E/AndroidRuntime(4312): at android.os.Looper.loop(Looper.java:137)
08-08 12:53:18.986: E/AndroidRuntime(4312): at android.app.ActivityThread.main(ActivityThread.java:5039)
08-08 12:53:18.986: E/AndroidRuntime(4312): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 12:53:18.986: E/AndroidRuntime(4312): at java.lang.reflect.Method.invoke(Method.java:511)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-08 12:53:18.986: E/AndroidRuntime(4312): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-08 12:53:18.986: E/AndroidRuntime(4312): at dalvik.system.NativeStart.main(Native Method)
教程视图布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/TutorialWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp" />
</LinearLayout>