由Theme.AppCompat引起的Android应用程序中的IllegalStateException

时间:2013-12-28 17:25:58

标签: java android nullpointerexception android-studio

当我尝试启动应用程序时,我得到了一个N​​PE。 我不知道是什么原因造成的。应用程序应在网站上显示地图。然后,用户可以轻松地将他的位置键入edittext并将其提交到网页。 这是我的代码:

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)

感谢您的帮助。

1 个答案:

答案 0 :(得分:6)

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