gpt4 book ai didi

java - getSupportActionBar 返回 null

转载 作者:行者123 更新时间:2023-12-01 04:23:05 26 4
gpt4 key购买 nike

我正在尝试使用 ActionBarSherlock 库在旧设备中使用操作栏。我的应用程序在 Android(4.2-真实设备) 上运行。但是,当我尝试在 Android 2.3.4 上运行的旧版 Galaxy 选项卡中打开它时,它崩溃了。这是我从 logcat

找到的

enter image description here

这是常规 Activity 的代码

    TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_general);

/*initial UI*/

initialUI();

/*Read file*/
String raw=readRawTextFile(this, R.raw.information);

/**/
MarkdownProcessor markDownPro = new MarkdownProcessor();

/**/
String getMarkDown = markDownPro.markdown(raw);

/**/
CharSequence cs = Html.fromHtml(getMarkDown);

/**/
text.setText(cs);

}

private void initialUI(){
text = (TextView)findViewById(R.id.text);
}

/**/

public static String readRawTextFile(Context ctx, int resId) {
InputStream inputStream=ctx.getResources().openRawResource(resId);
InputStreamReader inputreader=new InputStreamReader(inputStream);
BufferedReader buffreader=new BufferedReader(inputreader);
String line;
StringBuilder text=new StringBuilder();

try {
while ((line=buffreader.readLine())!=null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
return null;
}
return text.toString();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getSupportMenuInflater();

inflater.inflate(R.menu.general, menu);

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#003f84")));

return true;
}
}

常规 Activity 编号 82 是

 getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#003f84")));

这里是 list 文件的一些数据

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light.DarkActionBar" >

现在,另一个奇怪的是,如果它在 android 4.2 上运行,它不会显示我所描述的颜色

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#003f84")));

我需要提到的另一件事是,我在 tabactivity 中定义选项卡的类下面遇到了 NullPointerException。我有另一个类来显示 ListView 中的数据,在该类 ABS 中工作正常。因为,我在 Android 中使用选项卡导航。这是我的选项卡类。 (如果你需要的话)

public class Tabs extends TabActivity {
/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

setTabs() ;
}
private void setTabs()
{
addTab("Info", R.drawable.tab_home, GeneralActivity.class);
addTab("Program", R.drawable.tab_search, ScheduleMainActivity.class);

addTab("Abstracts", R.drawable.tab_home,AbstractActivity.class);
addTab("Map", R.drawable.tab_map,LocationMarkers.class);
}

private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);

spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}

}

这是我的样式文件

<resources>

<!--
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="android:Theme.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>

</resources>

最佳答案

如果您使用 ABS,那么为什么要使用 TabHost?您可以使用 ABS 中的 fragment 来创建选项卡。此外,应从 import android.support.v7.app.ActionBar

导入 getSupportActionBar

关于java - getSupportActionBar 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18755349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com