今天我升级到了API 21,这是本月最大的错误。
所以我花了一整天时间试图获得一个动作栏图标。
我正在使用带有sdk的eclipse并且更新了everthing。
问题如下:
在API 21之前,我添加了一个带
的图标android.app.ActionBar bar = getActionBar();
bar.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
一直都很好...... 但现在,不幸的是,这些行会导致Nullpointer异常......
(附加1)
所以我只能通过以下方式获取操作栏:
final ActionBar actionBar = getSupportActionBar();
那就是断点..这个actionBar没有设置图标,方法setIcon根本不起作用。
所以,为了找到解决方案,我已经完全重新安装了Eclipse和sdk并启动了一个新的android项目。在这个新项目中,也没有ActionBar图标,上面的代码也不起作用。
这让我想到这是一个新的API 21错误。
下面我发布了一些其他信息:
进口:
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
样式:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<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="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="Theme_Translucent" parent="Theme.AppCompat.Light">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:background">#3798db</item>
</style>
</resources>
使用第一个代码(附加1)的Logcat,抱歉布局错误:
erere`11-15 22:33:35.910: E/AndroidRuntime(2663): FATAL EXCEPTION: main
11-15 22:33:35.910: E/AndroidRuntime(2663): Process: com.lunation.kuhdwallpaper, PID: 2663
11-15 22:33:35.910: E/AndroidRuntime(2663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lunation.kuhdwallpaper/com.lunation.kuhdwallpaper.MainActivity}: java.lang.NullPointerException
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.os.Looper.loop(Looper.java:136)
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.app.ActivityThread.main(ActivityThread.java:5146)
11-15 22:33:35.910: E/AndroidRuntime(2663): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 22:33:35.910: E/AndroidRuntime(2663): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 22:33:35.910: E/AndroidRuntime(2663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
11-15 22:33:35.910: E/AndroidRuntime(2663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
11-15 22:33:35.910: E/AndroidRuntime(2663): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
11-15 22:33:35.910: E/AndroidRuntime(2663): at dalvik.system.NativeStart.main(Native Method)
11-15 22:33:35.910: E/AndroidRuntime(2663): Caused by: java.lang.NullPointerException
11-15 22:33:35.910: E/AndroidRuntime(2663): at com.lunation.kuhdwallpaper.MainActivity.onCreate(MainActivity.java:198)
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.app.Activity.performCreate(Activity.java:5231)
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 22:33:35.910: E/AndroidRuntime(2663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
11-15 22:33:35.910: E/AndroidRuntime(2663): ... 12 more
`
11-15 22:33:35.790: I/dalvikvm(2663): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
11-15 22:33:35.790: I/dalvikvm(2663): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
该项目使用appcompat v7,v4。
有人得到建议吗?
问候?
答案 0 :(得分:0)
在每个项目的末尾
添加如下项目
<!-- Support library compatibility-->
<item name="background">#3798db</item>
您可以自定义操作栏。它对我有用。