通过工具栏设置徽标

时间:2015-12-09 17:57:43

标签: android android-fragments android-actionbar toolbar

我有一个包含两个片段的Fragment活动。当我通过Fragment活动中的.setLogo(R.id.drawable)函数设置徽标时,会出现徽标,但问题出在下面 当我从第二个片段创建一个新活动时。然后在该活动中,当我申请.setLogo(R.id.drawable)时,它会给出错误

错误如下:

12-09 22:40:29.263    2969-2969/com.example.talha.test_fragement E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.talha.test_fragement, PID: 2969
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.talha.test_fragement/com.example.talha.test_fragement.Category_Blogs}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
        at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
        at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
        at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
        at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:204)
        at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
        at com.example.talha.test_fragement.Category_Blogs.onCreate(Category_Blogs.java:54)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)

新创建的活动它会出错,而不是设置徽标

public class Category_Blogs extends ActionBarActivity implements AbsListView.OnItemClickListener {

public static final String TAG = MainActivity.class.getSimpleName();
public final static String EXTRA_MESSAGE = "com.example.talha.appforblog.MESSAGE";
List<StackOverflowXmlParser.Entry> mainBlogPost = new ArrayList<StackOverflowXmlParser.Entry>();
private AbsListView mListView;
private ListView listView;
public String urlBlogtitle;
public String summarygetter;
public String urlBlogLink;
public String summaryContent;  // for storing content of the clicked item
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category__blogs);
    listView = (ListView) findViewById(android.R.id.list);
    Toolbar tool_bar1 = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(tool_bar1);
    tool_bar1.setLogo(R.drawable.ic_launcher);
    Intent intent = getIntent();
    String message = intent.getStringExtra(Tab2.EXTRA_MESSAGE);
    String link = message.trim() + "?feed=titles";
    Log.d("ye category click krne par next activity me ye link bnta hy parsing ke liye",link);
    loadPage(link);
}

主要碎片活动:此活动不会出现任何错误并显示徽标。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //loadPage();
    // Creating The Toolbar and setting it as the Toolbar for the activity

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    toolbar.setLogo(R.drawable.ic_launcher);
    //toolbar.setTitle(R.string.app_name);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(false); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

Tool_bar.xml 这包含工具栏xml

<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:label="@string/app_name"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android"

/>

我还在新创建的xml活动中包含tool_bar.xml,只在.java类中设置徽标但它也不起作用

1 个答案:

答案 0 :(得分:1)

这是你在Activity onCreate

上做的
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.drawable.logo_new);