美好的一天,我的应用程序被迫关闭,我得到这样的日志猫。 根据LogCat,我认为问题出在第51行,但我不知道为什么会出错。
第51行的代码为:
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00C4CD")));
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// c = MainActivity.this;
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00C4CD")));
logcat的
11-07 18:25:40.526: E/AndroidRuntime(17342): FATAL EXCEPTION: main
11-07 18:25:40.526: E/AndroidRuntime(17342): Process: com.fyp.atms, PID: 17342
11-07 18:25:40.526: E/AndroidRuntime(17342): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.atms/com.fyp.atms.MainActivity}: java.lang.NullPointerException
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.os.Handler.dispatchMessage(Handler.java:102)
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.os.Looper.loop(Looper.java:136)
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.app.ActivityThread.main(ActivityThread.java:5050)
11-07 18:25:40.526: E/AndroidRuntime(17342): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 18:25:40.526: E/AndroidRuntime(17342): at java.lang.reflect.Method.invoke(Method.java:515)
11-07 18:25:40.526: E/AndroidRuntime(17342): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
11-07 18:25:40.526: E/AndroidRuntime(17342): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
11-07 18:25:40.526: E/AndroidRuntime(17342): at dalvik.system.NativeStart.main(Native Method)
11-07 18:25:40.526: E/AndroidRuntime(17342): Caused by: java.lang.NullPointerException
11-07 18:25:40.526: E/AndroidRuntime(17342): at com.fyp.atms.MainActivity.onCreate(MainActivity.java:51)
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.app.Activity.performCreate(Activity.java:5242)
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-07 18:25:40.526: E/AndroidRuntime(17342): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
试图将此添加到我的值中 - > style.xml但仍无效。
<resources>
<style name="ActionBarTheme" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#00C4CD</item>
</style>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBarTheme</item>
</style>
</resources>
添加styles.xml后的LogCat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.atms/com.fyp.atms.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
答案 0 :(得分:2)
我遇到了这个问题,并提出了以下实现,这在我的案例中有用......
在AndroidManifest.xml中,在活动中使用Theme.AppCompat
样式。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat">
...
在MainActivity.java代码中,确保1)导入v7 ActionBarActivity
,2)使活动扩展ActionBarActivity
,3)使用相应的v7栏和getSupportActionBar()
方法
import android.support.v7.app.ActionBarActivity;
...
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.support.v7.app.ActionBar bar = getSupportActionBar();
if(bar != null){
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3f4deb")));
}
}
这基于http://developer.android.com/guide/topics/ui/actionbar.html。我无法使API 11及更高版本的方法起作用,但上述解决方案足以满足我的需求。
答案 1 :(得分:0)
尝试添加
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
在ActionBar bar = getActionBar(); 之前
答案 2 :(得分:0)
试试这个:
bar.setBackgroundColor(Color.parseColor("#00C4CD"));