添加TabListener时出现NullPointerException

时间:2012-06-25 13:19:14

标签: java android actionbarsherlock android-actionbar

我在LogCat中收到此错误消息:

  

06-25 15:17:34.495:E / AndroidRuntime(5891):   java.lang.RuntimeException:无法启动活动   ComponentInfo {com。示例/ com.example.MainActivity}:   的显示java.lang.NullPointerException   06-25 16:07:43.589:E / AndroidRuntime(6587):引起:java.lang.NullPointerException   06-25 16:07:43.589:E / AndroidRuntime(6587): at com.example.MyTabsListener.onTabSelected(MyTabListener.java:28)

这是我的主要活动:

package com.example;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;

public class MainActivity extends SherlockActivity {    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ActionBar actionbar = getSupportActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1 = actionbar.newTab().setText("Fragment A");
        SherlockFragment fragment1 = new AFragment();

        tab1.setTabListener(new MyTabsListener(fragment1, this
            .getApplicationContext()));
        actionbar.addTab(tab1);
    }

}

我简单的TabListener:

package com.example;    

import android.content.Context;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;  

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;

class MyTabsListener implements ActionBar.TabListener {
    public SherlockFragment fragment;
    private final Context appContext;

    public MyTabsListener(SherlockFragment fragment, Context ctx) {
        this.fragment = fragment;
        this.appContext = ctx;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(this.appContext, "Reselected!", Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }
}

最后我的班级片段A

package com.example;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;

public class AFragment extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.weekdays, container, false);
    }

}

所以这个函数导致了TabListener中的错误:

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
  ft.replace(R.id.fragment_container, fragment);
}

4 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,我想我已经解决了。

基本上,您的 MainActivity 应该扩展 SherlockFragmentActivity 而不是 SherlockActivity

答案 1 :(得分:2)

  1. MainActivity必须延长SherlockFragmentActivity(积分给JosipPranjic!)。
  2. 可以使用此SO问题中的以下代码修复 NullPointerException Null FragmentTransaction being passed to TabListener.onTabSelected()

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ignoredFt) {
      FragmentManager fragMgr = ((FragmentActivity) mActivity)
            .getSupportFragmentManager();
      FragmentTransaction ft = fragMgr.beginTransaction();
    
      // Check if the fragment is already initialized
      if (mFragment == null) {
          // If not, instantiate and add it to the activity
          mFragment = Fragment.instantiate(mActivity, mClass.getName());
    
          ft.add(android.R.id.content, mFragment, mTag);
      } else {
          // If it exists, simply attach it in order to show it
          ft.attach(mFragment);
      }
    
      ft.commit();
    }
    
    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ignoredFt) {
      FragmentManager fragMgr = ((FragmentActivity) mActivity)
              .getSupportFragmentManager();
      FragmentTransaction ft = fragMgr.beginTransaction();
    
      // Check if the fragment is already initialized
      if (mFragment == null) {
          // If not, instantiate and add it to the activity
          mFragment = Fragment.instantiate(mActivity, mClass.getName());
    
          ft.add(android.R.id.content, mFragment, mTag);
      } else {
          // If it exists, simply attach it in order to show it
          ft.detach(mFragment);
      }
    
      ft.commit();
    }
    

答案 2 :(得分:0)

看起来appContext未在代码中初始化......请初始化...

MyTabsListener

中使用
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();

但是如果可能的话,在MyTabsListener的构造函数中传递它而不是使那个静态

new MyTabsListener(fragment1,this)

答案 3 :(得分:0)

appContext未初始化。 请注意,类Activity扩展了类Context,因此,要显示Toast,就足够了

Toast.makeText(MainActivity, "Reselected!", Toast.LENGTH_LONG).show();

另一个问题:如果您想使用真实的应用程序上下文,请尝试getApplicationContext()。这将返回单例应用程序上下文。有关此问题的更多信息:link