I've been trying to use the cardslib lirary in a project I'm working on. I'm trying to use Thumbnail with CustomSource. Everything works as expected. However, when I call findViewById in setupInnerViewElements method on my class that extends Card to find R.id.textview_oferta_preco, it returns null.
Bellow follow my classs and my layouts.
Activity:
public class MenuOpcoes extends AppCompatActivity implements OfertasFragment.OnFragmentInteractionListener, NoticiasFragment.OnFragmentInteractionListener{
private Drawer.Result result = null;
private AccountHeader.Result headerResult = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_opcoes);
headerResult = new AccountHeader()
.withActivity(this)
.withHeaderBackground(R.drawable.header)
.addProfiles(
new ProfileDrawerItem().withName("Oswaldo Roberto Marques").withEmail("oswaldormarques#gmail.com").withIcon(getResources().getDrawable(R.drawable.profile))
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
result = new Drawer()
.withActivity(this)
.withTranslucentStatusBar(false)
.withActionBarDrawerToggle(true)
.withAccountHeader(headerResult)
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_listas).withIcon(FontAwesome.Icon.faw_list),
new PrimaryDrawerItem().withName(R.string.drawer_item_ofertas).withIcon(FontAwesome.Icon.faw_dollar),
new PrimaryDrawerItem().withName(R.string.drawer_item_noticias).withIcon(FontAwesome.Icon.faw_newspaper_o),
new SectionDrawerItem().withName(R.string.drawer_item_section_header),
new SecondaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {
if (drawerItem instanceof Nameable) {
String opcaoSelecionada = MenuOpcoes.this.getString(((Nameable) drawerItem).getNameRes());
FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (opcaoSelecionada.equals("Ofertas")) {
Toast.makeText(MenuOpcoes.this, MenuOpcoes.this.getString(((Nameable) drawerItem).getNameRes()), Toast.LENGTH_SHORT).show();
OfertasFragment ofertasFragment = new OfertasFragment();
transaction.replace(R.id.home_layout_container, ofertasFragment);
transaction.addToBackStack("ofertas");
}
if (opcaoSelecionada.equals(R.string.drawer_item_noticias)) {
Toast.makeText(MenuOpcoes.this, MenuOpcoes.this.getString(((Nameable) drawerItem).getNameRes()), Toast.LENGTH_SHORT).show();
NoticiasFragment noticiasFragment = new NoticiasFragment();
transaction.replace(R.id.home_layout_container, noticiasFragment);
transaction.addToBackStack("noticias");
}
transaction.commit();
}
}
}).build();
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_menu_opcoes, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
//int id = item.getItemId();
//noinspection SimplifiableIfStatement
//if (id == R.id.action_settings) {
// return true;
//}
//return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
this.finish();
}
}
#Override
public void onFragmentInteraction(Uri uri) {
}
CustomSource:
public class OfertasCardCustomSource extends Card {
public OfertasCardCustomSource(Context context) {
super(context);
init();
}
public OfertasCardCustomSource(Context context, int innerLayout) {
super(context, innerLayout);
init();
}
private void init() {
CardHeader header = new CardHeader(getContext());
header.setButtonOverflowVisible(true);
header.setTitle("Coca Cola 2,5L");
addCardHeader(header);
CardThumbnail thumbnail = new CardThumbnail(getContext());
thumbnail.setDrawableResource(R.drawable.coca_cola_25l);
addCardThumbnail(thumbnail);
}
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
TextView title = (TextView) parent.findViewById(R.id.textview_oferta_preco);
title.setText("R$ 4,25");
TextView subtitle = (TextView) parent.findViewById(R.id.textview_oferta_mercado);
subtitle.setText("Mercado Exemplo");
RatingBar mRatingBar = (RatingBar) parent.findViewById(R.id.carddemo_gplay_main_inner_ratingBar);
mRatingBar.setNumStars(5);
mRatingBar.setMax(5);
mRatingBar.setStepSize(0.5f);
mRatingBar.setRating(4.7f);
}
}
Fragment Layout:
<ScrollView
android:id="#+id/card_scrollview"
style="#style/carddemo_default_container_padding"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Custom Source Thumbnail-->
<TextView
style="#style/Theme.Carddemo.TitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/secao_ofertas"/>
<it.gmariotti.cardslib.library.view.CardViewNative
android:id="#+id/carddemo_thumb_customsource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/card_external"
card:card_layout_resourceID="#layout/native_card_thumbnail_layout"/>
<!-- Empty view-->
<View
android:layout_width="match_parent"
android:layout_height="15dp"/>
</LinearLayout>
</ScrollView>
CustomSource Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
>
<RatingBar
android:id="#+id/carddemo_gplay_main_inner_ratingBar"
style="#style/carddemo_myapps_main_inner_ratingbar"
android:numStars="5"
android:stepSize="1.0"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textview_oferta_preco"
android:layout_width="wrap_content"
android:textSize="12sp"
android:textColor="#android:color/holo_green_light"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textview_oferta_mercado"
android:textSize="12sp"
android:textColor="#BBB"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I don't know what to do. I'm using Android Studio. Thank you.
When extending Card, you have to call super by passing the layout reference you want to inflate.
I see in your OfertasCardCustomSource class that you have two constructors, one of them receiving a variable innerLayout which is correctly passed to the super class and another constructor that does not set the layout.
Your problem is probably one of the following:
You are not setting the layout in that constructor: you should do something like calling super(context, R.layout.the_layout_of_your_card) - check this reference for a more detailed example
You are not calling the constructor that receives innerLayout anywhere
What I recommend you to do is either delete the constructor that does not receive a layout to be inflated or to pass a default value exactly as it is done in the link I left above.
Related
I have three Fragments that I am trying to display by clicking the appropriate option from a Navigation Drawer. I have the Navigation Drawer displaying and opening/closing fine. But I can't get any of the Fragments to display in the Activity. The main screen just stays blank whenever I click an option. There are no compilation errors. I am new to Android development and also Java dev, and I've been trying to complete this project for a month so it will be finished as soon as I can get this to work. You may see some instances of redundant code, because I just wanted to try anything that could work and I am not sure what to delete, so any clarification on that would help too.
The Fragments should be displayed as results in a RecyclerView.
MainActivity:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nView;
private ActionBarDrawerToggle toggle;
private RequestQueue queue;
private List<ATM> atmList;
private RecyclerView atmView;
private AtmRecyclerViewAdapter atmRecyclerViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Find drawer layout
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// Find drawer view
nView = (NavigationView) findViewById(R.id.nav_view);
// Set up drawer view
setupDrawerContent(nView);
// Not sure what this does
nView.setNavigationItemSelectedListener(this);
toggle = new ActionBarDrawerToggle(
this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
// Tie DrawerLayout events to the ActionBarDrawerToggle
mDrawer.addDrawerListener(toggle);
queue = Volley.newRequestQueue(this);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
toggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
/*
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// The action bar home/up action should open or close the drawer.
switch (item.getItemId()) {
case android.R.id.home:
mDrawer.openDrawer(GravityCompat.START);
return true;
}
if (toggle.onOptionsItemSelected(item)) {
return true;
}
/* int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}*/
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_items_catalog) {
// Handle the items catalog action
} else if (id == R.id.nav_items_search) {
} else if (id == R.id.nav_atms) {
/*
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, Constants.atmURL,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject atmObj = response.getJSONObject(i);
ATM atm = new ATM();
atm.setAtmId(atmObj.getString("atmid"));
atm.setTitle(atmObj.getString("title"));
atm.setAddress(atmObj.getString("address"));
atm.setType(atmObj.getString("type"));
atm.setLat(atmObj.getDouble("lat"));
atm.setLon(atmObj.getDouble("lon"));
atm.setOnlineStatus(atmObj.getString("onlineStatus"));
atmList.add(atm);
// Log.d("ATM Statuses", atmList.get(i).getOnlineStatus());
}
// atmView = findViewById(R.id.atmRecyclerView);
// atmView.setHasFixedSize(true);
// atmView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
atmRecyclerViewAdapter = new AtmRecyclerViewAdapter
(MainActivity.this, atmList);
atmView.setAdapter(atmRecyclerViewAdapter);
atmRecyclerViewAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(jsonArrayRequest);
*/
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_items_catalog:
fragmentClass = ShowItems.class;
// TODO: Find out how to show the fragments in these calls
// fragment.listItems(???);
break;
case R.id.nav_items_search:
fragmentClass = SearchItems.class;
break;
case R.id.nav_atms:
fragmentClass = ShowATMs.class;
/*
atmView = findViewById(R.id.atmRecyclerView);
atmView.setHasFixedSize(true);
atmView.setLayoutManager(new LinearLayoutManager(this));
*/
break;
default:
fragmentClass = ShowItems.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
}
ShowATMs method - Want to show a list of ATM Objects in a RecyclerView format:
public class ShowATMs extends Fragment {
private RecyclerView atmView;
private AtmRecyclerViewAdapter atmRecyclerViewAdapter;
private RequestQueue queue;
// TODO: NEED TO CREATE A VIEW THAT WILL INFLATE CORRECT LAYOUT HERE
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Is this line needed?
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.atm_view, container, false);
atmView = rootView.findViewById(R.id.atmRecyclerView);
atmView.setHasFixedSize(true);
atmView.setLayoutManager(new LinearLayoutManager(getContext()));
final List<ATM> atmList = new ArrayList<>();
queue = Volley.newRequestQueue(getContext());
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, Constants.atmURL,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject atmObj = response.getJSONObject(i);
ATM atm = new ATM();
atm.setAtmId(atmObj.getString("atmid"));
atm.setTitle(atmObj.getString("title"));
atm.setAddress(atmObj.getString("address"));
atm.setType(atmObj.getString("type"));
atm.setLat(atmObj.getDouble("lat"));
atm.setLon(atmObj.getDouble("lon"));
atm.setOnlineStatus(atmObj.getString("onlineStatus"));
atmList.add(atm);
// Log.d("ATM Statuses", atmList.get(i).getOnlineStatus());
}
atmRecyclerViewAdapter = new AtmRecyclerViewAdapter
(getContext(), atmList);
atmView.setAdapter(atmRecyclerViewAdapter);
atmRecyclerViewAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(jsonArrayRequest);
return rootView;
}
}
XML Files
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment android:name="com.camelr.bilal.camelrecommerceproject.Fragments.ShowATMs"
android:id="#+id/ATMs"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout="#layout/atm_view" />
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!--<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#color/colorPrimary"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
atm_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<!--
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
-->
<android.support.v7.widget.RecyclerView
android:id="#+id/atmRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I have two other Fragments that I set up a little differently but just showing the one for ATMs because I feel it is the closest to working.
I have used the NavigationDrawer Template in my application but the Functions of the Intent not working properly. I already implemented the navigation listener in class declaration but still the intent navigation not working. This is the place I got stuck at the first time. I have searched many sites but nothing happened in my application. Can you help me? I added some extra comments please dont consider it
public class MainActivity extends AppCompatActivity
implements ViewPager.OnPageChangeListener {
/**
* Extra to add the the launch intent to specify that user comes from the notification (used to
* show not the current month but the last one)
*/
public static final String FROM_NOTIFICATION_EXTRA = "fromNotif";
/**
* List of first date of each month available
*/
private List<Date> dates;
/**
* TextView that displays the name of the month
*/
private TextView monthTitleTv;
/**
* Button to go the previous month
*/
private Button previousMonthButton;
/**
* Button to go the next month
*/
private Button nextMonthButton;
/**
* ViewPager used to display each month in a Fragment
*/
private ViewPager pager;
/**
* The current {#link #pager} position
*/
private int selectedPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.monthly_report_progress_bar);
final View content = findViewById(R.id.monthly_report_content);
monthTitleTv = (TextView) findViewById(R.id.monthly_report_month_title_tv);
previousMonthButton = (Button) findViewById(R.id.monthly_report_previous_month_button);
nextMonthButton = (Button) findViewById(R.id.monthly_report_next_month_button);
pager = (ViewPager) findViewById(R.id.monthly_report_view_pager);
previousMonthButton.setText("<");
nextMonthButton.setText(">");
previousMonthButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (selectedPosition > 0) {
selectPagerItem(selectedPosition - 1, true);
}
}
});
nextMonthButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (selectedPosition < dates.size() - 1) {
selectPagerItem(selectedPosition + 1, true);
}
}
});
UIHelper.removeButtonBorder(previousMonthButton);
UIHelper.removeButtonBorder(nextMonthButton);
// Load list of monthly asynchronously since it can take time
new AsyncTask<Void, Void, List<Date>>() {
#Override
protected List<Date> doInBackground(Void... params) {
return DateHelper.getListOfMonthsAvailableForUser(MainActivity.this);
}
#Override
protected void onPostExecute(List<Date> dates) {
if (isFinishing()) {
return;
}
MainActivity.this.dates = dates;
configureViewPager();
progressBar.setVisibility(View.GONE);
content.setVisibility(View.VISIBLE);
}
}.execute();
}
private void configureViewPager() {
pager.setOffscreenPageLimit(0);
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
#Override
public Fragment getItem(int position) {
return new MonthlyReportFragment(dates.get(position));
}
#Override
public int getCount() {
return dates.size();
}
});
pager.addOnPageChangeListener((ViewPager.OnPageChangeListener) this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav_camera:
Intent i = new Intent(MainActivity.this, Monthly_ExpenseEdit_activity.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent i1 = new Intent(MainActivity.this, ExpenseEditActivity.class);
startActivity(i1);
break;
}
return false;
}
});
}
/**
* Extra to add the the launch intent to specify that user comes from the notification (used to
* show not the current month but the last one)
*/
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void selectPagerItem(int position, boolean animate)
{
pager.setCurrentItem(position, animate);
onPageSelected(position);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
/**
* Extra to add the the launch intent to specify that user comes from the notification (used to
* show not the current month but the last one)
*/
#Override
public void onPageSelected(int position)
{
selectedPosition = position;
Date date = dates.get(position);
monthTitleTv.setText(DateHelper.getMonthTitle(this, date));
// Last and first available month
boolean last = position == dates.size() - 1;
boolean first = position == 0;
/**
* Extra to add the the launch intent to specify that user comes from the notification (used to
* show not the current month but the last one)
*/
nextMonthButton.setEnabled(!last);
nextMonthButton.setTextColor(ContextCompat.getColor(this, last ? R.color.monthly_report_disabled_month_button : android.R.color.white));
previousMonthButton.setEnabled(!first);
previousMonthButton.setTextColor(ContextCompat.getColor(this, first ? R.color.monthly_report_disabled_month_button : android.R.color.white));
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
XML:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MonthlyReportActivity">
<ProgressBar android:id="#+id/monthly_report_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true" />
<LinearLayout android:id="#+id/monthly_report_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="47dp"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center_vertical"
android:background="#color/primary_dark">
<Button
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button android:id="#+id/monthly_report_previous_month_button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="24dp"
android:background="#drawable/calendar_month_switcher_button_drawable" />
<TextView android:id="#+id/monthly_report_month_title_tv"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textSize="21dp"
android:textColor="#android:color/white"
android:textAllCaps="true"
android:gravity="center" />
<Button android:id="#+id/monthly_report_next_month_button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="24dp"
android:background="#drawable/calendar_month_switcher_button_drawable" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/monthly_report_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
in your onNavigationItemSelected() method return true instead of false
Also make sure the id exist in navigation drawer menu.
app:menu="#menu/menu_navigation"
use this code:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav_camera:
Intent i = new Intent(MainActivity.this, Monthly_ExpenseEdit_activity.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent i1 = new Intent(MainActivity.this, ExpenseEditActivity.class);
startActivity(i1);
break;
}
return true; //change here
}
I'm trying to show a nested fragment inside my fragment on button click, so on button .setOnClickListener (which works fine with Log.d) of my main fragment i've placed:
Fragment noteFragment = new FrontPageNote();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.frontNote, noteFragment).commit();
as found on developer guide, where FrontPageNote is my nested fragment (which obviously extends Fragment) and R.id.frontNote is the id of the xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="48dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:layout_weight="1"
android:id="#+id/frontNote"
android:background="#color/Background">
<LinearLayout
android:id="#+id/linearLayoutFront"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:paddingTop="15dp">
<EditText
android:id="#+id/titleEditFront"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/titleEdit"
android:textColor="#color/TextColor.White"/>
<EditText
android:id="#+id/noteEditFront"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/noteEdit"
android:paddingTop="20dp"
android:textColor="#color/TextColor.White"
android:inputType="textMultiLine" />
</LinearLayout>
</RelativeLayout>
the nested fragment FrontPageNote also is linked to this xml using getActivity().setContentView(R.layout.front_note); under onCreate() method.
Q. what am I doing wrong? thanks
EDIT: added FrontPageNote.class:
public class FrontPageNote extends Fragment {
// Declare Variables
public static long rowID;
private EditText title_edit;
private EditText note_edit;
private static final String TITLE = "title";
private static final String NOTE = "note";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().setContentView(R.layout.front_note);
// Locate the EditText in add_note.xml
title_edit = (EditText) getActivity().findViewById(R.id.titleEditFront);
note_edit = (EditText) getActivity().findViewById(R.id.noteEditFront);
// Retrieve the Row ID from ViewNote.java
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
rowID = extras.getLong("row_id");
title_edit.setText(extras.getString(TITLE));
note_edit.setText(extras.getString(NOTE));
}
}
// Create an ActionBar menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_all_geofences, menu);
menu.add("Save changes")
.setOnMenuItemClickListener(this.SaveButtonClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
super.onCreateOptionsMenu(menu, inflater);
}
// Capture save menu item click
OnMenuItemClickListener SaveButtonClickListener = new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// Passes the data into saveNote() function
if (title_edit.getText().length() != 0) {
AsyncTask<Object, Object, Object> saveNoteAsyncTask = new AsyncTask<Object, Object, Object>() {
#Override
protected Object doInBackground(Object... params) {
saveNote();
return null;
}
#Override
protected void onPostExecute(Object result) {
// Close this activity
//getActivity().finish();
}
};
// Execute the saveNoteAsyncTask AsyncTask above
saveNoteAsyncTask.execute((Object[]) null);
}
else {
// Display a simple alert dialog that forces user to put in a title
AlertDialog.Builder alert = new AlertDialog.Builder(
getActivity());
alert.setTitle("Title is required");
alert.setMessage("Put in a title for this note");
alert.setPositiveButton("Okay", null);
alert.show();
}
return false;
}
};
// saveNote() function
private void saveNote() {
DatabaseConnector dbConnector = new DatabaseConnector(getActivity());
if (getActivity().getIntent().getExtras() == null) {
// Passes the data to InsertNote in DatabaseConnector.java
dbConnector.InsertNote(title_edit.getText().toString(), note_edit
.getText().toString());
} else {
// Passes the Row ID and data to UpdateNote in DatabaseConnector.java
dbConnector.UpdateNote(rowID, title_edit.getText().toString(),
note_edit.getText().toString());
}
}
}
Simple timepicker function is failing with Fatal Exception - NullPointerException and java.lang.reflect.InvocationTargetException on following line :
int hour = time_picker.getCurrentHour();
Fragment_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.calapp1.MainActivity$PlaceholderFragment" >
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<TextClock
android:id="#+id/textClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextClock" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
android:text="Show Time"
android:onClick="setTime"
/>
MainActivity.java :
public class MainActivity extends Activity {
TimePicker time_picker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time_picker = (TimePicker) findViewById(R.id.timePicker1);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void setTime(View v)
{
int hour = time_picker.getCurrentHour();
int minute = time_picker.getCurrentMinute();
try{
Toast.makeText(getBaseContext(), "select time is "+hour, Toast.LENGTH_LONG).show();
}
catch(Exception e){
e.printStackTrace();
Toast.makeText(getBaseContext(), "problem", Toast.LENGTH_LONG).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Can you please assist?
Thank you,
Yogesh.
You timepicker is in the fragment layout and not in the activity layout. Activity onCreate() is too early to find it in the activity view hierarchy.
Move the findViewById() to fragment onCreateView() and change it to rootView.findViewById().
Please be noted that you have your timepicker in Fragment_main.xml and you're trying it in the onCreate where the contentview is set as setContentView(R.layout.activity_main); so please move the line to onCreateView() and use rootView.findViewById(). That will srely solve the issue.
So here's the problem. I'm coding an app with two EditText views inside a fragment and a button. When the button is clicked, it changes the text of the EditText. In the onViewCreated() method I use this code to get the EditText instance and store it to a variable.
EditText box1 = (EditText)(getView.findViewById(R.id.box1));
This works fine. However, when I try to access variable box1 when the button is clicked, the EditText has become null and throws a NullPointerException. Does anyone know why this would happen?
Here is the fragment code:
public class MyFragment extends Fragment {
EditText box1, box2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_text_coder_free,
container, false);
//Works here.
box1 = (EditText)(rootView.findViewById(R.id.box1));
box2 = (EditText)(rootView.findViewById(R.id.box2));
//Same thing. Sets the text correctly
box1.setText("hey look at me!");
return rootView;
}
//Called when the button is clicked.
public void setBox1Text(String text) {
//It's null here so it skips to the else
if(box1 != null) {
box1.setText(text);
}
else {
//This doesn't work. Throws the exception here.
box1 = (EditText)(getView().findViewById(R.id.box1));
}
}
public void setBox2Text(String text) {
if(box2 != null) {
box2.setText(text);
}
else {
//This doesn't work. Throws the exception here.
box2 = (EditText)(getView().findViewById(R.id.box2));
}
}
public String getBox1Text() {
if(box1 == null) {
return "Nice try.";
}
return box1.getText().toString();
}
public String getBox2Text() {
if(box2 == null) {
return "Nice try.";
}
return box2.getText().toString();
}
}
Here is the activity which houses the fragment:
public class MyActivity extends Activity {
MyFragment myFragment
EditText box1, box2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_coder);
if (savedInstanceState == null) {
myFragment = new MyFragment();
getFragmentManager().beginTransaction()
.add(R.id.container, myFragment.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_app, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void box1Click(View v) {
myFragment.setBox2Text("Some text");
}
public void box2Click(View v) {
myFragment.setBox2Text("Some text");
}
}
And here is the XML from the fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.mycode.MyFragment" >
<EditText
android:id="#+id/box1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:inputType="textMultiLine" />
<EditText
android:id="#+id/box2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/box1"
android:inputType="textMultiLine|none"
android:focusable="false" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/box2"
android:layout_alignParentRight="true"
android:onClick="box2Click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/encrypt_button_text"
android:layout_below="#id/box2"
android:layout_toLeftOf="#id/button2"
android:onClick="box1Click" />
</RelativeLayout>
Exemple of use:
public class main extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.core_info_tab, container, false);
ImageButton ib = (ImageButton) view.findViewById(R.id.imageButton1);
return view;
}
}
Make sure the fragment is attached to the activity before calling setBox1Text(String text) else the view is not initialized and getView returns null.
There is no need to initialize the edittext again.
You can just have
public void setBox1Text(String text) {
box1.setText(text); // box1 is already initialized in onCreate no need to initialize again
}
If you want use getView use it in onActivtiyCreated
box1 = (EditText)getView().findViewById(R.id.box1));
That's because you're trying to execute setBoxXText(...) before having created the fragment, onCreateView is not yet executed. Be sure that your fragment is loaded before access it's views.
If you check your code, the only way to box1 & box2 to be null is that onCreateView is not yet called, so when you do
if(box1 != null) {
box1.setText(text);
}
else {
and it goes through the else statement, it's because you didn't initialize the fragment.
You have several ways to fix this, for example you can set this values in the fragment initialization (1) or use a listener to notify when the fragment is loaded (2).
1-
Use a factory to initialize your fragment, fragments in android need empty constructors, so in this way you will use an empty constructor and also ensure yourself that in onCreateView you will have setted the box1 and box2 strings
public class YourFragment extends Fragment{
//Empty and private constructor
private YourFragment(){
}
private String box1Text;
private String box2Text;
public void setBox1Text(String box1Text){
this.box1Text = box1Text;
}
public void setBox2Text(String box2Text){
this.box2Text = box2Text;
}
public static YourFragment YourFragmentFactory(String box1Text, String box2Text){
YourFragment result = new YourFragment();
result.setBox1Text(box1Text);
result.setBox2Text(box2Text);
return result;
}
}
and 2:
use a listener to notify your activity when you can start to set the texts:
public interface YourFragmentListener{
public void onViewCreated();
}
private YourFragmentListener listener;
public void setListener(YourFragmentListener listener){
this.listener = listener;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
if(this.listener != null){
this.listener.onViewCreated();
}
}
To use findViewById you have to execute it on Context (eg. an Activity).
Use: View.getContext().getViewById...
You can also try cleaning the project