IllegalStateException 在我的 Android 应用程序中由 Theme.AppCompat 引起 [英] IllegalStateException in my Android app caused by Theme.AppCompat

查看:23
本文介绍了IllegalStateException 在我的 Android 应用程序中由 Theme.AppCompat 引起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试启动应用程序时,我得到了一个 NPE.我不知道是什么原因造成的.该应用程序应在网站上显示地图.然后用户可以轻松地在编辑文本中输入他的位置并将其提交到网页.这是我的代码:

I get a NPE when I try to launch the App. I don't know what causes it. The App should show a map at a website. Then the User can easily type his location into the edittext and submit it to the webpage. Here is my Code:

package com.timbremer.iimv;

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

private WebView wv;
private EditText etlocation;
private Button btclose;
private Button btgo;
private boolean send;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    send = false;


    etlocation = (EditText) findViewById(R.id.etlocation);
    btgo.setOnClickListener(new View.OnClickListener() {

        private String location;


        @Override
        public void onClick(View v) {
             location = etlocation.getText().toString();

     WebView wv = (WebView) findViewById(R.id.wv);
            wv.getSettings().setBuiltInZoomControls(true);
            CookieManager.getInstance().setAcceptCookie(true);
            wv.getSettings().setJavaScriptEnabled(true);
            wv.getSettings().setLoadWithOverviewMode(true);
            wv.getSettings().setUseWideViewPort(true);
            wv.loadUrl("http://www.ingress.com/intel");
            wv.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
                @Override
                public void onPageFinished(WebView view, String url) {
                    if(!send) {
                        view.loadUrl("javascript:{" +
                                "document.getElementById('adress').value='" + location + "';" +
                                "var form = document.getElementsByName('login');" +
                                "form[0].submit();};"
                        );
                        send = true;
                    }
                }


            });
        }
    });
    }
}

这是我的 LogCat:

Here is my LogCat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.timbremer.iimv/com.timbremer.iimv.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
        at android.app.ActivityThread.access$800(ActivityThread.java:145)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5081)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
        at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
        at com.timbremer.iimv.MainActivity.onCreate(MainActivity.java:36)

感谢您的帮助.

推荐答案

You need to use a Theme.AppCompat theme (or descendant) with this activity.

你有

public class MainActivity extends ActionBarActivity

您正在使用支持库中的 ActionBar,并且您的活动需要有 Theme.AppCompat

You are using ActionBar from support library and you need to have Theme.AppCompat for your activity

为清单文件中的活动应用 AppCompat 主题.

Apply AppCompat theme for the activity in manifest file.

<activity android:theme="@style/Theme.AppCompat

请检查以下链接中的添加操作栏

Please check Adding Actionbar in the below link

http://developer.android.com/guide/topics/ui/actionbar.html

也查看此博客

http://android-developers.blogspot.in/2013/08/actionbarcompat-and-io-2013-app-source.html

还有 btgo 没有初始化会导致 NullPointerException

Also btgo is not initialized which will cause NullPointerException

这篇关于IllegalStateException 在我的 Android 应用程序中由 Theme.AppCompat 引起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆