actionbarsherlock sherlockfragment google maps v2 is null - actionbarsherlock

I'm trying to use Actionbarsherlock with a tab bar and in one of those taps i want a map.
I have successfully created an map in a sherlockfragment with google map v2, but I want add markers programmatically to the map.
public class Map_fragment extends SherlockFragment {
static final LatLng Cph = new LatLng(55.702355,12.436523);
private final static String MAP_TAG = "map";
private GoogleMap mMap;
SupportMapFragment mMapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
SupportMapFragment map = (SupportMapFragment) (getSherlockActivity().getSupportFragmentManager().findFragmentByTag(MAP_TAG));
if(map==null){
Log.d("MAP", "NULL");
}
if(map!=null){
Log.d("MAP", " " + map);
Log.d("GETMAP", " " + map.getMap());
}
//GoogleMap mMap = map.getMap();
//mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Cph, 14));
return inflater.inflate(R.layout.mapv2_layout, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
public void setupMap(){
Log.d("SUCCESS" , "SUCCESS");
}
}
I have tried with many different suggestions from other sites and posts on stack overflow, but the map is always null.
I have tried with onCreate and onAttach after suggestions from others, but with no luck.
I have tried for a long time and Im about to pull my hair out :(

I solved it by manually adding the SherlockMapFragment class to the ActionBarSherlock library, found here http://code.google.com/p/opendatakit/source/browse/src/com/actionbarsherlock/app/SherlockMapFragment.java?spec=svn.tables.35872cc920b5668de433c6fc81efc86c4970bac1&repo=tables&r=0c1b0212e5b8bb5af8ce2b8e9357cd30e3870084
Then with the following lines i finally got "gMap" that wasn't null
SupportMapFragment map;
map = (SupportMapFragment) (getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.map));
gMap = map.getMap();

Related

Step Counter in Android Studio doesn't count steps

I have a problem with an app I'm trying to develop in Android Studio. I'm trying to develop a step counter using the STEP_COUNTER sensor in a Fragment.I decided to show my results in a TextView ('mStepsSinceReboot'), but it keeps being empty. Do you have any idea on how to make it work?
An hypotesis is that I should use a Service, but online I only found implementations that don't require it.
Thanks in advance for your answer. This is the code of my Fragment.
public class StepFragment extends Fragment implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mStepCounter;
private boolean isSensorPresent;
private TextView mStepsSinceReboot;
public StepFragment() {
// Required empty public constructor
}
public static StepFragment newInstance(String param1, String param2) {
StepFragment fragment = new StepFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_step, container, false);
mStepsSinceReboot=v.findViewById(R.id.stepssincereboot);
mSensorManager=(SensorManager) this.getActivity().getSystemService(getContext().SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
mStepCounter=mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
isSensorPresent=true;
}
else{
isSensorPresent=false;
}
return v;
}
#Override
public void onSensorChanged(SensorEvent event) {
mStepsSinceReboot.setText(("Steps since reboot: "+String.valueOf(event.values[0])));
Log.d("Passi", String.valueOf(mStepsSinceReboot));
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onResume(){
super.onResume();
if(isSensorPresent){
mSensorManager.registerListener(this,mStepCounter,SensorManager.SENSOR_DELAY_NORMAL);
}
}
#Override
public void onPause(){
super.onPause();
if(isSensorPresent){
mSensorManager.unregisterListener(this);
}
}
#Override
public void onDestroy(){
super.onDestroy();
mSensorManager=null;
mStepCounter=null;
}
}
you added the permission in the manifest file ?
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

Cannot infer arguments (unable to resolve constructor)

I am trying to make a program with a tablayout, each tab representing a fragment. In the second fragment (AddFragment), im want to make a list that displays the strings I entered in the EditText. But its giving me the error above. More detailed:
error: cannot infer type arguments for ArrayAdapter<>
itemsAdapter = new ArrayAdapter<>(AddFragment.this, android.R.layout.simple_list_item_1, items);
Code for the AddFragment class:
public class AddFragment extends Fragment {
EditText etPlan;
EditText etExercise;
ImageButton imageButton;
ArrayList<String> items;
ArrayAdapter<String> itemsAdapter;
ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_add, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
findViews();
super.onViewCreated(view, savedInstanceState);
}
private void findViews() {
listView = getView().findViewById(R.id.listView);
etPlan = getView().findViewById(R.id.etPlan);
etExercise = getView().findViewById(R.id.etExercise);
imageButton = getView().findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
items = new ArrayList<>();
itemsAdapter = new ArrayAdapter<>(AddFragment.this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(itemsAdapter);
setUpListViewListener();
}
private void setUpListViewListener() {
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Context context = getContext();
Toast.makeText(context, "Item Removed", Toast.LENGTH_LONG).show();
items.remove(position);
itemsAdapter.notifyDataSetChanged();
return true;
}
});
}
});
}
}
The declaration that you have works if working directly in the activity JAVA file. While working in the fragment, you need to establish clearly the type of argument and provide the correct context.
The following should work (assuming that you are passing strings):
itemsAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);

How do I put the adapter into the viewpager?

This is the code of MyPageAdapter
public class MyPageAdapter extends PagerAdapter {
int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11, R.drawable.img12, R.drawable.img13
, R.drawable.img14, R.drawable.img15, R.drawable.img16, R.drawable.img17, R.drawable.img18, R.drawable.img19, R.drawable.img20, R.drawable.img21, R.drawable.img22, R.drawable.img23, R.drawable.img24
, R.drawable.img25, R.drawable.img26, R.drawable.img27, R.drawable.img28, R.drawable.img29, R.drawable.img30};
private LayoutInflater inflater;
private Context context;
public MyPageAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == ((LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment_slideshow, container, false);
EditText editText = v.findViewById(R.id.editTextNumberDecimal);
ImageView imageView = (ImageView)v.findViewById(R.id.imageView2);
imageView.setImageResource(images[position]);
int text = position+1;
editText.setText(text);
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.invalidate();
}
}
AND this is the code of SlideshowFragment.
public class SlideshowFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
ViewPager2 viewPager2 = (ViewPager2) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager2.setAdapter(adapter);
return root;
}
}
I would like to add a view pager to the slide fragment so that 30 photos can be viewed as a slide show.
But I can't do setAdapter in viewpager.
Really, is there anything other than a way to make and put each fragment for each of the 30 pictures?
Please let me know how to modify my code.
You are mixing Viewpager and Viewpager2 these are implemented in 2 totally different ways.
try
ViewPager viewPager = (ViewPager) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager.setAdapter(adapter);
and change your xml to also be a ViewPager widget.
Alternatively you could probably change your adapter to a RecyclerView.Adapter https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter
As Viewpager2 is just a customised RecyclerView.

Android Studio - same menu for all activities (Navigation Drawer)

I'm new to Android Studio (and also pretty new to javascrips), so this might be a stupid question.
I do not want to maintain the same code more than once if possible, which is why I am trying to create a navigation-menu, to be used in all my activities.
So far I have created a "BaseActivity" that contains all the functions for my menu. This one works fine.
Now my problem:
In my other activities I can not get it to show up (not without issues anyway). It seems to me like it overwrites the layout.
My base ("BaseActivity"):
public class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.string.Open, R.string.Close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView NavigationView=(NavigationView)findViewById(R.id.navigation_view);
NavigationView.setNavigationItemSelectedListener(this);
}
#Override
public void setContentView(int layoutResID)
{
super.setContentView(R.layout.activity_base);
ViewGroup content = (ViewGroup) findViewById(R.id.navigation_view);
getLayoutInflater().inflate(layoutResID, content, true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if(id==R.id.nav_Home) {
startActivity(new Intent(this, MainActivity.class));
}
else
if(id==R.id.nav_Activity1) {
startActivity(new Intent(this, Activity1.class));
}
return super.onOptionsItemSelected(item);
}
}
If I change the app to start through this activity the menu works, but the layout seems to be overwritten (it's empty).
When I try to run it normally (MainActivity), I get some issues.
MainActivity:
public class MainActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }}
The menu-icon is there, but I can not click on it.
If I remove the code in my MainActivity it works as before (with the layout problem).
I have tried something like this:
public class MainActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup content = (ViewGroup) findViewById(R.id.navigation_view);
getLayoutInflater().inflate(R.layout.activity_main, content, true);
}}
Not working well. The layout from MainActivity seems to be implemented in the menu. It looks very strange.
Any ideas how to solve my problem?

ActionBar.Tablistener deprecated

I'm trying to build a swipe tabs view by using fragment activity and actionbar.tablistener. I have referenced other people tutorial and do it, however, it keep on deprecated and do not successful run it. Then I changed to AppComBatActivity and it do not works, show viewgroup error which I don't know why. THen I changed to ActionBarActivity, it worked finally, but no matter how I edit my tab activity, in java class and xml too, it does not show anything.
I have actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) deprecated as well.
I have attached my code as following...
fragmentmenu.class
public class FragmentMenu extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
FragmentPageAdapter ft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_menu);
viewPager = (ViewPager)findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
actionBar = getActionBar();
viewPager.setAdapter(ft);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Menu").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Report").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Setting").setTabListener(this));
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
</code>
FragmentPagerAdapter
<code>
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new MyActivity();
case 1:
return new Report();
case 2:
return new Setting();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
</code>
One of the fragment
<code>
public class Report extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.report, container, false);
}
}
}}
Please help!
ActionBar.Tablistener is deprecated. You need to use the TabLayout instead.
Check this tutorial for more infos :
http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

Resources