尝试在左侧显示带有应用徽标的ActionBar,屏幕中心标题和右侧菜单图标。下面的类扩展了ListActivity,我该如何做到这一点:
我创建的方法是centerlogo();请参阅下面的代码摘录:
public class NewsActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ListView list;
String[] itemname = { "Bafana crash out of Afcon",
"Mali and Guinea face ultimate lottery",
"Orlando Pirates eye cup glory",
"Ivory Coast advance to Afcon quarters",
"Algeria qualify for Afcon quarter-finals",
"Reflect on Afcon lessons - Mbalula",
"Tovey preaches patience with Bafana",
"SuperSport's Brockie harbours lofty goals" };
Integer[] imgid = { R.drawable.bafana, R.drawable.mailguinea,
R.drawable.orlando, R.drawable.ivorycoast, R.drawable.algeria,
R.drawable.reflection, R.drawable.tovey, R.drawable.supersport, };
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
setListAdapter(adapter);
centreLogo();
}
private void centreLogo() {
// TODO Auto-generated method stub
Drawable d = getResources().getDrawable(R.drawable.banner);
android.app.ActionBar bar = getActionBar();
bar.setBackgroundDrawable(d);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowCustomEnabled(true);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
bar.setCustomView(R.layout.news_view);
bar.setDisplayShowHomeEnabled(true);
bar.setIcon(R.drawable.icon);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public void readMore(View view) {
Intent read = new Intent(NewsActivity.this, ReadMoreActivity.class);
startActivity(read);
}
}
我收到以下错误:
01-29 13:45:40.093: E/AndroidRuntime(15402): FATAL EXCEPTION: main
01-29 13:45:40.093: E/AndroidRuntime(15402): java.lang.RuntimeException: Unable to start activity ComponentInfo{platinum.platinumstars/platinumnews.NewsActivity}: java.lang.NullPointerException
01-29 13:45:40.093: E/AndroidRuntime(15402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
01-29 13:45:40.093: E/AndroidRuntime(15402): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
01-29 13:45:40.093: E/AndroidRuntime(15402): at android.app.ActivityThread.access$700(ActivityThread.java:150)
01-29 13:45:40.093: E/AndroidRuntime(15402): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
01-29 13:45:40.093: E/AndroidRuntime(15402): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 13:45:40.093: E/AndroidRuntime(15402): at android.os.Looper.loop(Looper.java:176)
01-29 13:45:40.093: E/AndroidRuntime(15402): at android.app.ActivityThread.main(ActivityThread.java:5279)
01-29 13:45:40.093: E/AndroidRuntime(15402): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 13:45:40.093: E/AndroidRuntime(15402): at java.lang.reflect.Method.invoke(Method.java:511)
答案 0 :(得分:1)
我决定采用GareginSargsyan的建议。主要问题是我试图膨胀默认的android列表,所以我实例化了一个新的ListView, 设置列表视图的ContentView,其余为历史记录。
public class NewsActivity extends ActionBarActivity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_platinum_news_list);
String[] itemname = { "Bafana crash out of Afcon",
"Mali and Guinea face ultimate lottery",
"Orlando Pirates eye cup glory",
"Ivory Coast advance to Afcon quarters",
"Algeria qualify for Afcon quarter-finals",
"Reflect on Afcon lessons - Mbalula",
"Tovey preaches patience with Bafana",
"SuperSport's Brockie harbours lofty goals" };
Integer[] imgid = { R.drawable.bafana, R.drawable.mailguinea,
R.drawable.orlando, R.drawable.ivorycoast, R.drawable.algeria,
R.drawable.reflection, R.drawable.tovey, R.drawable.supersport, };
// CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
// setListAdapter(adapter);
CustomListAdapter test = new CustomListAdapter(this, itemname, imgid);
list = (ListView) findViewById(R.id.list);
list.setAdapter(test);
centreLogo();
}
private void centreLogo() {
// TODO Auto-generated method stub
Drawable d = getResources().getDrawable(R.drawable.banner);
getSupportActionBar().setBackgroundDrawable(d);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setCustomView(R.layout.news_view);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.icon);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
getMenuInflater().inflate(R.menu.newsmenu, menu);
return true;
}
public void readMore(View view) {
Intent read = new Intent(NewsActivity.this, ReadMoreActivity.class);
startActivity(read);
}
}
答案 1 :(得分:0)
最有可能的是,getActionBar()导致异常。请尝试使用getSupportActionBar()。