我正在创建使用webview打开网页的android应用程序,但在运行时我收到了错误。这是移动js页面,我想在其中打开webview。
Log-cat:
06-19 00:44:25.756: I/Process(1130): Sending signal. PID: 1130 SIG: 9
06-19 00:46:45.136: D/AndroidRuntime(1156): Shutting down VM
06-19 00:46:45.136: W/dalvikvm(1156): threadid=1: thread exiting with uncaught exception (group=0xb4a48ba8)
06-19 00:46:45.156: E/AndroidRuntime(1156): FATAL EXCEPTION: main
06-19 00:46:45.156: E/AndroidRuntime(1156): Process: com.example.dynakodetechnologypvt.ltd, PID: 1156
06-19 00:46:45.156: E/AndroidRuntime(1156): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dynakodetechnologypvt.ltd/com.example.dynakodetechnologypvt.ltd.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.os.Handler.dispatchMessage(Handler.java:102)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.os.Looper.loop(Looper.java:136)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-19 00:46:45.156: E/AndroidRuntime(1156): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 00:46:45.156: E/AndroidRuntime(1156): at java.lang.reflect.Method.invoke(Method.java:515)
06-19 00:46:45.156: E/AndroidRuntime(1156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-19 00:46:45.156: E/AndroidRuntime(1156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-19 00:46:45.156: E/AndroidRuntime(1156): at dalvik.system.NativeStart.main(Native Method)
06-19 00:46:45.156: E/AndroidRuntime(1156): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
06-19 00:46:45.156: E/AndroidRuntime(1156): at com.example.dynakodetechnologypvt.ltd.MainActivity.onCreate(MainActivity.java:19)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.Activity.performCreate(Activity.java:5231)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-19 00:46:45.156: E/AndroidRuntime(1156): ... 11 more
主要活动
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
String url = "http://www.google.com";
WebView view = (WebView) this.findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dynakodetechnologypvt.ltd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<activity
android:name="com.example.dynakodetechnologypvt.ltd.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>
</manifest>
如何解决此问题请帮助
答案 0 :(得分:1)
ActionBarActivity
需要一个从Theme.AppCompat
延伸的主题,否则您的应用会因此错误而崩溃:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
因此,在清单文件中,使用带有 MainActivity 的新主题:as
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dynakodetechnologypvt.ltd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<activity
android:name="com.example.dynakodetechnologypvt.ltd.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.AppCompat"> //Add here
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这会在Actionbar
中添加MainActivity
但通常您需要自定义Actionbar
。要执行此操作,您需要使用Theme.AppCompat
父级创建两种样式,例如&#34; @style/Theme.AppCompat.Light
&#34;。第一个将用于api 11>=
(在android操作栏中构建的android版本),第二个用于api 7-10
(没有在操作栏中构建)。
让我们来看看第一种风格。它将位于 res / values-v11 / styles.xml 中。它看起来像这样:
<style name="Theme.Styled" parent="@style/Theme.AppCompat.Light">
<!-- Setting values in the android namespace affects API levels 11+ -->
<item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
<!-- Setting values in the android namespace affects API levels 11+ -->
<item name="android:background">@drawable/ab_custom_solid_styled</item>
<item name="android:backgroundStacked"
>@drawable/ab_custom_stacked_solid_styled</item>
<item name="android:backgroundSplit"
>@drawable/ab_custom_bottom_solid_styled</item>
</style>
你需要为api 7-10拥有相同的风格。它将位于 res / values / styles.xml 中,但因为api级别还不了解原始的android操作栏样式项,我们应该使用一个,由支持库提供。 ActionBar Compat项目的定义与原始android一样,但没有&#34; android:&#34;部分在前面:
<style name="Theme.Styled" parent="@style/Theme.AppCompat.Light">
<!-- Setting values in the default namespace affects API levels 7-11 -->
<item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
<!-- Setting values in the default namespace affects API levels 7-11 -->
<item name="background">@drawable/ab_custom_solid_styled</item>
<item name="backgroundStacked">@drawable/ab_custom_stacked_solid_styled</item>
<item name="backgroundSplit">@drawable/ab_custom_bottom_solid_styled</item>
</style>
有关详细信息,请参阅ActionBarCompat and I/O 2013 App Source