使用AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tvt.webviewdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="tvt.webviewdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
但我有一个错误:java.lang.NoClassDefFoundError:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tvHello = (TextView) findViewById(R.id.tv_hello);
mCallback = new ActionMode.Callback() { // error line <<<<<
/**
* Invoked whenever the action mode is shown. This is invoked
* immediately after onCreateActionMode
*/
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
/** Called when user exits action mode */
@Override
public void onDestroyActionMode(ActionMode mode) {
mMode = null;
}
/**
* This is called when the action mode is created. This is called by
* startActionMode()
*/
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Demo");
getMenuInflater().inflate(R.menu.context_menu, menu);
return true;
}
/** This is called when an item in the context menu is selected */
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action1:
Toast.makeText(getBaseContext(), "Selected Action1 ",
Toast.LENGTH_LONG).show();
mode.finish(); // Automatically exists the action mode, when
// the user selects this action
break;
case R.id.action2:
Toast.makeText(getBaseContext(), "Selected Action2 ",
Toast.LENGTH_LONG).show();
break;
case R.id.action3:
Toast.makeText(getBaseContext(), "Selected Action3 ",
Toast.LENGTH_LONG).show();
break;
}
return false;
}
};
OnLongClickListener listener = new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mMode != null)
return false;
else
mMode = startActionMode(mCallback);
return true;
}
};
tvHello.setOnLongClickListener(listener);
}
编辑 - 错误日志:
04-11 20:45:24.600: E/AndroidRuntime(348): FATAL EXCEPTION: main
04-11 20:45:24.600: E/AndroidRuntime(348): java.lang.NoClassDefFoundError: tvt.webviewdemo.MainActivity$1
04-11 20:45:24.600: E/AndroidRuntime(348): at tvt.webviewdemo.MainActivity.onCreate(MainActivity.java:28)
04-11 20:45:24.600: E/AndroidRuntime(348): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-11 20:45:24.600: E/AndroidRuntime(348): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-11 20:45:24.600: E/AndroidRuntime(348): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-11 20:45:24.600: E/AndroidRuntime(348): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-11 20:45:24.600: E/AndroidRuntime(348): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-11 20:45:24.600: E/AndroidRuntime(348): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 20:45:24.600: E/AndroidRuntime(348): at android.os.Looper.loop(Looper.java:123)
04-11 20:45:24.600: E/AndroidRuntime(348): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-11 20:45:24.600: E/AndroidRuntime(348): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 20:45:24.600: E/AndroidRuntime(348): at java.lang.reflect.Method.invoke(Method.java:507)
04-11 20:45:24.600: E/AndroidRuntime(348): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-11 20:45:24.600: E/AndroidRuntime(348): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-11 20:45:24.600: E/AndroidRuntime(348): at dalvik.system.NativeStart.main(Native Method)
如何解决问题?
谢谢!
答案 0 :(得分:1)
我认为在onCreateActionMode()
:
getMenuInflater().inflate(R.menu.context_menu, menu);
应该使用ActionMode对象进行膨胀:
mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);