현재 Android 버전 4.x 이상에서 작동하는 응용 프로그램이 있습니다. 2.x와 같은 오래된 장치에서 사용할 수 있도록 변경하려고합니다. 작업 표시 줄을 제외한 모든 항목이 정상적으로 작동합니다. getSupportActionBar를 시도 할 때마다 널 포인터 예외가 발생합니다. 이것은 포럼에서 많이 논의 된 문제이지만 아직 해결할 수 없습니다.안드로이드 - 오래된 장치에 대한 AppCompat/ActionBar 사용 (java.lang.NullPointerException)
응용 프로그램은 appCompat 라이브러리를 사용하지만 이전 장치에서는 작동하지 않습니다. 지원되는 행위를 얻는 액션 바에게
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auction_list);
if (android.os.Build.VERSION.SDK_INT >=11)
{
getActionBar().setIcon(R.drawable.gem);
getActionBar().setDisplayHomeAsUpEnabled(false);
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar_title, null);
((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);
}
else
{
//This calls a private class inside the same class to get a supported action bar
notActionBar nab = new notActionBar();
nab.getNotActionBar(); //THIS IS THE ERROR LINE
}
개인 클래스를 얻으려고 노력
styles.xml 파일
<style name="AppBaseTheme" parent="Theme.MyApp">
<item name="android:spinnerItemStyle">@style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
<item name="android:windowNoTitle">true</item>
</style>
MyApp.xml 파일
<style name="Theme.MyApp" parent="@style/Theme.AppCompat">
<item name="actionBarItemBackground">@drawable/selectable_background</item>
<item name="popupMenuStyle">@style/PopupMenu.MyApp</item>
<item name="dropDownListViewStyle">@style/DropDownListView.MyApp</item>
<item name="actionBarTabStyle">@style/ActionBarTabStyle.MyApp</item>
<item name="actionDropDownStyle">@style/DropDownNav.MyApp</item>
<item name="actionBarStyle">@style/ActionBar.Transparent.MyApp</item>
<item name="actionModeBackground">@drawable/cab_background_top</item>
<item name="actionModeSplitBackground">@drawable/cab_background_bottom</item>
<item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.MyApp</item>
활동 이온 바. 저는 모든 활동이 파편을 확장하기 때문에 ActionBarActivity를 확장 할 수 없기 때문에 액션 바를 직접 가져 오는 대신 개인 클래스를 사용했습니다.
private class notActionBar extends ActionBarActivity {
public void getNotActionBar(){
ActionBar actionBar = getSupportActionBar(); //NULL POINTER EXCEPTION
actionBar.setIcon(R.drawable.gem);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayShowCustomEnabled(true);
this.getSupportActionBar().setDisplayShowCustomEnabled(true);
this.getSupportActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar_title, null);
((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle());
//assign the view to the actionbar
this.getSupportActionBar().setCustomView(v);
}
public void setTitleActionBar(String a){
getSupportActionBar().setTitle(a);
}
}
입니다. 액션 바가 프래그먼트를 확장하지 않는다고 생각했기 때문에 내부 클래스로 모든 것을 수행했습니다. 그것은 잘 작동 지금 나는 oncreate 메서드에서 작업 표시 줄을 얻고있다. – user3328051
쿨! 나는 이것이 당신을 위해 일해서 행복합니다 :-) – donfuxx