问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/20817309/illegalstateexception-in-my-android-app-caused-by-theme-appcompat