我已经查看了多个答案并尝试了他们的解决方案,但我仍然无法在我的应用程序中显示。我正在使用Fragment,因为我的应用程序有多个选项卡。这是我的代码:
package edu.uah.smap.smapscanner.tabsswipe;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Tab1Fragment extends ListFragment
{
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(getActivity(), item + " selected", Toast.LENGTH_LONG).show();
}
}
代替getActivity()
,我尝试了
getActivity().getActivityContext()
,Tab1Fragment.this.getActivity()
和Tab1Fragment.this.getActivity().getActivityContext()
这些都没有奏效。
以下是MainTabs活动代码:
package edu.uah.smap.smapscanner;
import edu.uah.smap.smapscanner.tabsswipe.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
public class MainTabs extends FragmentActivity
implements ActionBar.TabListener
{
private ViewPager m_viewPager;
private TabsPagerAdapter m_adapter;
private ActionBar m_actionBar;
// Tab titles
private String[] tabs = { "Check-in/Check-out", "My Items", "Lab Inventory" };
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_main);
// Initialization
m_viewPager = (ViewPager) findViewById(R.id.pager);
m_actionBar = getActionBar();
m_adapter = new TabsPagerAdapter(getSupportFragmentManager());
m_viewPager.setAdapter(m_adapter);
m_viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
//on changing the page make the respected tab selected
m_actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
});
m_actionBar.setHomeButtonEnabled(false);
m_actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs)
{
m_actionBar.addTab(m_actionBar.newTab().setText(tab_name).setTabListener(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
// on tab selected show respected fragment view
m_viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
}
}
答案 0 :(得分:4)
尝试使用getActivity().getApplicationContext()
另请参阅this answer。何时使用Activity上下文以及何时使用Application Context存在差异。
离开Activity
的范围时,请使用应用程序上下文。在某些情况下,您必须使用活动上下文,否则您将获得例外,在其他情况下,您应使用应用程序上下文以避免泄露Activity
上下文。
答案 1 :(得分:2)
Toast.makeText(getApplicationContext(), "Your Message", Toast.LENGTH_LONG).show();