0

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

06-25 15:17:34.495:E/AndroidRuntime(5891):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example/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

4 回答 4

4

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

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

于 2012-06-25T19:32:12.967 回答
2
  1. MainActivity 必须扩展SherlockFragmentActivity(感谢 JosipPranjic!)。
  2. NullPointerException可以使用此 SO 问题中的以下代码修复:Null FragmentTransaction being passing 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();
    }
    
于 2012-06-26T19:44:57.570 回答
0

看起来 appContext 没有在代码中初始化......请初始化那个......

用于MyTabsListener

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

但是如果可能的话,将它传递给 MyTabsListener 的构造函数,而不是让它成为静态的

new MyTabsListener(fragment1,this)
于 2012-06-25T13:58:41.630 回答
0

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

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

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

于 2012-06-25T14:20:58.300 回答