egalStateException异常在我的Andr​​oid

egalStateException异常在我的Andr​​oid

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

问题描述

我收到了NPE,当我尝试启动应用程序。我不知道是什么原因造成的。该应用程序应显示在网站上的地图。然后,用户可以轻松地输入自己的位置进入的EditText并将其提交到该网页。这是我的code:

 包com.timbremer.iimv;

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

公共类MainActivity扩展ActionBarActivity {

私人的WebView WV;
私人的EditText etlocation;
私人按钮btclose;
私人按钮btgo;
私人布尔发送;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    发送= FALSE;


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

        私人字符串的位置;


        @覆盖
        公共无效的onClick(视图v){
             位置= etlocation.getText()的toString();

     的WebView WV =(web视图)findViewById(R.id.wv);
            。wv.getSettings()setBuiltInZoomControls(真正的);
            。CookieManager.getInstance()setAcceptCookie(真正的);
            wv.getSettings()setJavaScriptEnabled(真)。
            。wv.getSettings()setLoadWithOverviewMode(真正的);
            。wv.getSettings()setUseWideViewPort(真正的);
            wv.loadUrl(http://www.ingress.com/intel);
            wv.setWebViewClient(新WebViewClient(){

                @覆盖
                公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
                    view.loadUrl(URL);
                    返回true;
                }
                @覆盖
                公共无效onPageFinished(web视图查看,字符串URL){
                    如果(!发送){
                        view.loadUrl(JavaScript的:{+
                                的document.getElementById(联系地址)值='+位置+; +
                                VAR形式= document.getElementsByName('登录'); +
                                形式[0] .submit();};
                        );
                        发送= TRUE;
                    }
                }


            });
        }
    });
    }
}
 

下面是我的LogCat中:

 了java.lang.RuntimeException:无法启动活动ComponentInfo {com.timbremer.iimv / com.timbremer.iimv.MainActivity}:java.lang.IllegalStateException:您需要使用Theme.AppCompat主题(或后代)与本次活动。
        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
        在android.app.ActivityThread.access $ 800(ActivityThread.java:145)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1206)
        在android.os.Handler.dispatchMessage(Handler.java:102)
        在android.os.Looper.loop(Looper.java:136)
        在android.app.ActivityThread.main(ActivityThread.java:5081)
        在java.lang.reflect.Method.invokeNative(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:515)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:781)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        在dalvik.system.NativeStart.main(本机方法)
 致:java.lang.IllegalStateException:您需要使用Theme.AppCompat主题(或后代)与本次活动。
        在android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
        在android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
        在android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
        在com.timbremer.iimv.MainActivity.onCreate(MainActivity.java:36)
 

感谢您的帮助。

解决方案

 您需要使用Theme.AppCompat主题(或后代)与本次活动。
 

 公共类MainActivity扩展ActionBarActivity
 

您正在使用从支持库动作条,你需要有Theme.AppCompat为你的活动

应用AppCompat主题清单文件的活动。

 <活动机器人:主题=@风格/ Theme.AppCompat
 

请检查下面的链接添加动作条

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异常

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;
                    }
                }


            });
        }
    });
    }
}

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)

Thanks for your help.

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

You have

public class MainActivity extends ActionBarActivity

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

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

Also check this blog

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

Also btgo is not initialized which will cause NullPointerException

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

08-20 14:30