【问题标题】:IllegalStateException in my Android app caused by Theme.AppCompat我的 Android 应用程序中的 IllegalStateException 由 Theme.AppCompat 引起
【发布时间】:2014-01-16 00:38:04
【问题描述】:

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

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:

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)

感谢您的帮助。

【问题讨论】:

  • 我在任何地方都没有看到NullPointerException
  • btgo 未初始化,但应该是 NPE。

标签: java android nullpointerexception android-studio


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

你有

public class MainActivity extends ActionBarActivity

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

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

<activity android:theme="@style/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

【讨论】:

  • 谢谢@Raghunandan。现在工作!但我的 WebView 不显示网页,只是空白。
  • @tbremer19 新问题 发布新问题,评论区不是提问的最佳场所
  • 好吧对不起...我累了...忘记授予互联网权限
猜你喜欢
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 2012-02-27
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
相关资源
最近更新 更多