pager Cannot resolved as a variable - java

I'm trying to make action bar tabs, but I got a error:"pager Cannot resolved as a variable..."
Thansk for your help.
Java code;
package olcay.akgn.fibonaccicalculator;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity implements TabListener {
android.app.ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1=actionBar.newTab();
tab1.setText("Hesaplayıcı");
tab1.setTabListener(this);
Tab tab2=actionBar.newTab();
tab2.setText("Bölünme");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("AKGN","onTabReselected at "+" position"+tab.getPosition()+"name"+tab.getText());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Log.d("AKGN","onTabReselected at "+" position"+tab.getPosition()+"name"+tab.getText());
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Log.d("AKGN","onTabReselected at "+" position"+tab.getPosition()+"name"+tab.getText());
}
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
Fragment fragment=null;
if(arg0==0){
fragment=new Hesapla();
}
if(arg0==0){
fragment=new Bolunme ();
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
Note; I begin this project with this video tutorial;
https://www.youtube.com/watch?v=iEl0ylVvZho
I tried to "Create "pager" in type id " but it did not solved my issue. Please help me.

You need to declare a ViewPager widget on your activity_main layout.
Never edit the R class - it is automatically generated.

Related

Why does my tablayout always do the current tab selected and the next tab?

I have an activity that uses a tablayout with 3 tabs. When the activity is started, it initializes (onAttach, OnCreate, OnCreatView) for tabs 1 and 2. When I select tab 2, it initializes tab 3. Does tablayout automatically do this for a reason or is there a way to tell it not to get ahead of itself.
The following is the activity java code
package com.grgapps.checkingin;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import static androidx.constraintlayout.widget.Constraints.TAG;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
public class CheckInNew extends AppCompatActivity implements
CheckInNewGeneral.OnFragmentInteractionListener,
CheckInNewFrequency.OnFragmentInteractionListener,
CheckInNewRecipients.OnFragmentInteractionListener,
CheckInNewCurrentLocation.OnFragmentInteractionListener,
CheckInNewDestination.OnFragmentInteractionListener {
private AdView mAdView;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context = context;
setContentView(R.layout.activity_check_in_new);
Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
Log.i(TAG, "CheckInNew onCreate: ");
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adViewCheckInNewCurrentLocation);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
mAdView.loadAd(adRequest);
DrawerUtil.getDrawer(this,toolbar);
configureTabLayout();
Log.i(TAG, "CheckInNew onCreate: Completed OnCreate");
}
private void configureTabLayout() {
TabLayout tabLayout = findViewById(R.id.check_in_main_new_tab_layout);
Log.i(TAG, "CheckInNew configureTabLayout: ");
tabLayout.addTab(tabLayout.newTab().setText("General"));
tabLayout.addTab(tabLayout.newTab().setText("Frequency"));
tabLayout.addTab(tabLayout.newTab().setText("Recipients"));
tabLayout.addTab(tabLayout.newTab().setText("Current Location"));
tabLayout.addTab(tabLayout.newTab().setText("Destination"));
final ViewPager viewPager = findViewById(R.id.check_in_new_main_pager);
final PagerAdapter adapter = new CheckInNewMainPagerAdapter
(getSupportFragmentManager(),
tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new
TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public void onFragmentInteraction(Uri uri) {
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Log.i(TAG, "CheckInNew onCreateOptionsMenu: ");
getMenuInflater().inflate(R.menu.check_in_new, menu);
return true;
}
}
The following is the pager adapter for the tabs.
package com.grgapps.checkingin;
import android.util.Log;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import static androidx.constraintlayout.widget.Constraints.TAG;
public class CheckInMainPagerAdapter extends FragmentPagerAdapter {
int tabCount;
public CheckInMainPagerAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
Log.i(TAG, "CheckInMainPagerAdapter: ");
this.tabCount = numberOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Log.i(TAG, "CheckInMainPagerAdapter Case 0 getItem: ");
return new CheckInRecentList();
case 1:
Log.i(TAG, "CheckInMainPagerAdapter Case 1 getItem: ");
return new CheckInApproveRequested();
case 2:
Log.i(TAG, "CheckInMainPagerAdapter Case 2 getItem: ");
return new CheckInSearch();
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
}
It is because of your ViewPager offscreen page limit. By default, it is 1. It means the right and left fragments are loaded. Please refer to the documentation for more information
https://developer.android.com/reference/android/support/v4/view/ViewPager#setoffscreenpagelimit

Error incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager

Error Error incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager is drving me nuts.
In my app, I have 3 navigation drawer item, timesheet,claim and view, created in MainActivity. Now I wanted to add two tabs with swipe view in view item.
MainActivity // for navigation drawer
import android.app.Fragment;
import android.app.FragmentManager;
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new TimeSheet();
break;
case 1:
fragment=new Claims1();
break;
case 2:
fragment=new Viewview();
break;
default:
break;
}
TabsFragmentPagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsFragmentPagerAdapter extends FragmentPagerAdapter {
public TabsFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch(index) {
case 0:
return new UpdatePage2();
case 1:
return new Receipt();
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
ViewView.java
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.app.ActionBar;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.project.project.database.MyDatabaseHelper;
public class ViewView extends Fragment implements ActionBar.TabListener {
InfoAPI sqlcon;
private SimpleCursorAdapter dataAdapter;
private SQLiteDatabase database;
private MyDatabaseHelper dbHelper;
private ListView listView;
private ViewPager viewPager;
private TabsFragmentPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = {"Information", "receipt"};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View View1 = inflater.inflate(R.layout.viewview1, container, false);
listView = (ListView) View1.findViewById(R.id.listView1);
dbHelper = new MyDatabaseHelper(getActivity());
sqlcon = new InfoAPI(getActivity());
viewPager = (ViewPager) View1.findViewById(R.id.pager);
mAdapter = new TabsFragmentPagerAdapter(getActivity().getFragmentManager()); //here the error
viewPager.setAdapter(mAdapter);
actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 0; i < 2; i++) {
actionBar.addTab(actionBar.newTab().setText(tabs[i]).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
BuildList();
return View1;
}
Problem :
Error:(49, 81) error: incompatible types: android.app.FragmentManager
cannot be converted to android.support.v4.app.FragmentManager
If changed import android.support.v4.app.FragmentManager; in TabsFragmentPagerAdapter to import android.app.FragmentManager;, the first issue solve but get this Error:(12, 15) error: incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager...I have no idea how to fix ..
Edited
MainActivity
import android.app.Fragment;
import android.support.v4.app.FragmentManager;
case 2:
fragment=new ViewView();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
Error
Error:(148, 30) error: incompatible types: ViewView cannot be
converted to Fragment Error:(156, 69) error: incompatible types:
android.app.FragmentManager cannot be converted to
android.support.v4.app.FragmentManager Error:(157, 80) error:
incompatible types: android.app.Fragment cannot be converted to
android.support.v4.app.Fragment
Change
import android.app.Fragment;
import android.app.FragmentManager;
to
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
in every class. you are facing problems because in your Fragment creation class you are using support v4 fragment and in your MainActivity class you are inflating as a simple fragment.
You also need to change getFragmentManager() to getSupportFragmentManager(), and make sure they're extending a FragmentActivity class.
Hope it will help you.

Destroy activity containing two fragments after clicking Andorid Back Button

Problem SOLVED!
ISSUE:
I have problem, and I can't find any solution. I have Maps_Activity, which contains two fragments. Both fragments show google maps v2 and I display them on screen thanks to two ActionBar tabs.
The problem is appearing when I want to return to previous activity. When I click Android back button, my whole application turns down, shows pupup, that my application was stopped.
What I should add to my code to remove this error?
Solution:
Maps_Activity.java
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import com.google.android.gms.maps.MapFragment;
public class Maps_Activity extends Activity {
public static FragmentManager manager;
Fragment fragmentTab1 = new Medical_Shops_Map();
Fragment fragmentTab2 = new Medical_Clicnics_Map();
Button Medical_Shops_Button, Medical_Clinics_Button;
ActionBar actionBar ;
#Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_maps_);
manager = getFragmentManager();
actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("Apteki 24H/7");
tab1.setTabListener(new MyTabsListener(fragmentTab1));
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("Przychodnie Lekarskie");
tab2.setTabListener(new MyTabsListener(fragmentTab2));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
protected class MyTabsListener implements ActionBar.TabListener {
private Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
ft = manager.beginTransaction();
ft.replace(R.id.map_container, fragment);
ft.commit();
Log.e("Activity", "Fragment replace");
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// some people needed this line as well to make it work:
MapFragment mMap2 = ((MapFragment) Maps_Activity.manager.findFragmentById(R.id.map));
ft = manager.beginTransaction();
if(mMap2!=null)
{
ft.remove(mMap2);
ft.commit();
Log.e("Activity", "Mapa_fragment destroy");
}
else
{
MapFragment mMap1 = ((MapFragment) Maps_Activity.manager.findFragmentById(R.id.map1));
ft.remove(mMap1);
ft.commit();
Log.e("Activity", "Mapa_fragment2 destroy");
}
}
}
}
One of the fragments:
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class Medical_Clicnics_Map extends Fragment implements OnMapReadyCallback {
private static View view;
private static GoogleMap mMap2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.medical__clicnics__map, container, false);
setUpMapIfNeeded();
return view;
}
public void setUpMapIfNeeded() {
MapFragment mMap2 = ((MapFragment) Maps_Activity.manager
.findFragmentById(R.id.map1));
mMap2.getMapAsync(Medical_Clicnics_Map.this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d("Activity", "Map ready");
googleMap.setMyLocationEnabled(true);
LatLng sydney = new LatLng(-33.867, 151.206);
googleMap.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
}
Maybe for someone this code will be helpful ;)

Animation on Main Activity

I want to make animation on my main activity..
when my app is opened i want to open with animation ..
I have posted my main activity code and i want bounce animation while opening my pager xml.
import android.app.ActionBar;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class DesignMainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_design_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
requestWindowFeature(Window.FEATURE_NO_TITLE);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Login").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Register").setTabListener(tabListener));
}
}
**My Questions :
1.I want bounce Animation While opening my app.
2.I want to hide title bar.**

Unable to launch app - java.lang.NullPointerException [duplicate]

This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
I'm having problems launching my app (crashes), while there is no errors in code.
This happend after i converted code of List View activity to List Fragment...
My main activity:
package com.example.eronetmarket;
import com.preporuceno_app.AppAdapter;
import android.R.drawable;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.itemmenu, menu);
return true;
}
ViewPager viewPager=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
final ActionBar actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED));
//actionBar.setDisplayShowHomeEnabled(false);
addTabs(actionBar);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
Log.d("VIVZ","onPageScrolled "+i+" "+v+" "+i2);
}
#Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
Log.d("VIVZ","onPageSelected "+i);
}
#Override
public void onPageScrollStateChanged(int i) {
if(i==ViewPager.SCROLL_STATE_IDLE)
Log.d("VIVZ","onPageScrollStateChanged scroll state idle "+i);
if(i==ViewPager.SCROLL_STATE_DRAGGING)
Log.d("VIVZ","onPageScrollStateChanged scroll state dragging "+i);
if(i==ViewPager.SCROLL_STATE_SETTLING)
Log.d("VIVZ","onPageScrollStateChanged scroll state settling "+i);
}
});
}
private void addTabs(ActionBar actionBar)
{
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("PREPORUČENO");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("NAJPOPULARNIJE");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText("KATEGORIJE");
tab3.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
// Log.d("VIVZ","onTabSelected "+tab.getText());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// Log.d("VIVZ","onTabUnselected "+tab.getText());
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// Log.d("VIVZ","onTabReselected "+tab.getText());
}
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment=null;
if(i==0)
{
fragment=new FragmentA();
}
if(i==1)
{
fragment=new Preporuceno();
}
if(i==2)
{
fragment=new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
And my fragment list:
package com.example.eronetmarket;
import java.io.FileNotFoundException;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class Preporuceno extends ListFragment {
private AppAdapter mAdapter;
private ListView siteList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("mobAppModel", "OnCreate()");
View rootView = inflater.inflate(R.layout.activity_preporuceno, container, false);
siteList = (ListView) getActivity().findViewById(R.id.listView1);
siteList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
String url = mAdapter.getItem(pos).getstoreURL();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
if(isNetworkAvailable()){
Log.i("mobAppModel", "starting download Task");
AppDownloadTask download = new AppDownloadTask();
download.execute();
}else{
mAdapter = new AppAdapter(getActivity().getApplicationContext(), -1, XMLsourcePullParser.getmobAppModel(getActivity()));
siteList.setAdapter(mAdapter);
}
return rootView;
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private class AppDownloadTask extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... arg0) {
//Download the file
try {
Downloader.DownloadFromUrl("http://minores.info/joomla30/stacksites.xml", getActivity().openFileOutput("XMLsource.xml", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result){
//setup our Adapter and set it to the ListView.
mAdapter = new AppAdapter(getActivity().getApplicationContext(), -1, XMLsourcePullParser.getmobAppModel(getActivity()));
siteList.setAdapter(mAdapter);
Log.i("mobAppModel", "adapter size = "+ mAdapter.getCount());
}
}
}
Here is my LogCat messages:
In line 38 where LogCat shows error, I have this line public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
I don't understand where I went wrong?
Change this
siteList = (ListView) getActivity().findViewById(R.id.listView1);
to
siteList = (ListView) rootView.findViewById(R.id.listView1);
The view belongs to the inflated layout and you need to use the view object to initialize views.
java.lang.RuntimeException: Your content must have a ListView whose
id attribute is 'android.R.id.list'
ListActivity or ListFragment will have listview which occuppies the entire screen. If you want to have other views in the layout then your layout must have listview with id andorid.R.id.list
http://developer.android.com/reference/android/app/ListFragment.html
QUoting docs
ListFragment has a default layout that consists of a single list view.
However, if you desire, you can customize the fragment layout by
returning your own view hierarchy from onCreateView(LayoutInflater,
ViewGroup, Bundle). To do this, your view hierarchy must contain a
ListView object with the id "#android:id/list" (or list if it's in
code)

Categories