开发者

Android ActionBar and Cursor

开发者 https://www.devze.com 2023-04-09 01:19 出处:网络
public class TaskDetailTabHome extends Activity implements ActionBar.TabListener{ String taskid, empid, ac;
public class TaskDetailTabHome extends Activity implements ActionBar.TabListener{
String taskid, empid, ac;
private DbAdapter_Assignment assignment;


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tasktab);

       开发者_StackOverflow社区 taskid = getIntent().getExtras().getString("TaskID");
        empid = getIntent().getExtras().getString("EmpID");
        ac = getIntent().getExtras().getString("AC");

         ActionBar bar = getActionBar();
         bar.addTab(bar.newTab().setText("TASK").setTabListener(this));        
         bar.addTab(bar.newTab().setText("COMMENT").setTabListener(this));        
         bar.addTab(bar.newTab().setText("FLIGHT").setTabListener(this));        

         bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);        
         bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
         bar.setDisplayShowHomeEnabled(true);        
         bar.setDisplayShowTitleEnabled(false);

         assignment =  new DbAdapter_Assignment(getBaseContext());
         assignment.open();


 }
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {

}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    if(tab.getText()== "TASK")
    {
        setContentView(R.layout.tasktab);
        Cursor c  = assignment.taskDetails(taskid, empid);  ??????????
    }

}

I am trying to pupulate the view with SQLite data. But the cursor is throwing an error(NullPointerException). The same cursor is working when I am not using ActionBar(tabs). what am I doing wrong ?


I suppose the problem is here :

assignment =  new DbAdapter_Assignment(getBaseContext());
assignment.open();

And more exactly here getBaseContext() I think it's the only line where you can have problems (thinking of course that taskid and empid are not null of course) . Search for the exact use of getBaseContext() .


You can try this. I guess you're working with tabs and not getting the right context. You should go up in the context until you get the last parent and try to get it done.

public static Context goUp(Activity current){
        if(current.getParent()!=null){
            current=current.getParent();
            goUp(current);
        }
        return current.getBaseContext();
    }

assignment =  new DbAdapter_Assignment(goUp(TaskDetailTabHome.this));
assignment.open();
0

精彩评论

暂无评论...
验证码 换一张
取 消