I have a fragment which hosts a TabLayout and a ViewPager. The host fragment is part of a container that switches based on the item selection of a bottom navigation bar.
For some reason, the ViewPager isn't inflating the fragments, and the TabLayout is unresponsive. When sliding left or right, the indicator stops as soon as you stop sliding, and doesn't snap like it should. And, of course, the fragments don't show.
Here is my code...I'm wondering what I am doing wrong.
PopularHolderFragment.java
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ai.gab.android.R;
import ai.gab.android.ui.adapters.fragment.PopularFragmentsAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by Andrew Quebe on 10/20/2017.
*/
#SuppressWarnings("ConstantConditions")
public class PopularHolderFragment extends Fragment {
#BindView(R.id.tabLayout)
TabLayout tabLayout;
#BindView(R.id.viewPager)
ViewPager viewPager;
public static PopularHolderFragment newInstance() {
return new PopularHolderFragment();
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_popular_holder, container, false);
ButterKnife.bind(this, view);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewPager.setAdapter(new PopularFragmentsAdapter(getActivity().getSupportFragmentManager()));
tabLayout.addTab(tabLayout.newTab().setText("Posts"));
tabLayout.addTab(tabLayout.newTab().setText("Users"));
tabLayout.setupWithViewPager(viewPager);
}
}
PopularFragmentsAdapter.java
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import ai.gab.android.ui.fragments.popular.PopularPostsFragment;
import ai.gab.android.ui.fragments.popular.PopularUsersFragment;
/**
* Created by Andrew Quebe on 8/7/2017.
*/
public class PopularFragmentsAdapter extends FragmentStatePagerAdapter {
public PopularFragmentsAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return PopularPostsFragment.newInstance();
case 1:
return PopularUsersFragment.newInstance();
}
return null;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Posts";
case 1:
return "Users";
}
return super.getPageTitle(position);
}
#Override
public int getCount() {
return 2;
}
}
fragment_popular_holder.xml
<android.support.design.widget.CoordinatorLayout 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=".ui.activities.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Light">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="2dp"
tools:targetApi="lollipop" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/util_popular_main_content" />
</android.support.design.widget.CoordinatorLayout>
util_popular_main_content.xml
<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:showIn="#layout/fragment_popular_holder">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
Screenshots
Also wondering why the text isn't its normal size.
Note that I do not want a toolbar in this layout, and perhaps that is my issue, but I don't see why it would be as the widgets should be able to work independently of each other.
Using the comment from Mike and AndroidSmoker74's answer, I was able to fix the TabLayout bug.
PopularWrapperFragment.java
public class PopularWrapperFragment extends Fragment {
#BindView(R.id.tabLayout)
TabLayout tabLayout;
#BindView(R.id.viewPager)
ViewPager viewPager;
public static PopularWrapperFragment newInstance() {
return new PopularWrapperFragment();
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_popular_holder, container, false);
ButterKnife.bind(this, view);
((MainActivity) getActivity()).setPagingEnabled(false);
viewPager.setAdapter(new PopularFragmentsAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
return view;
}
}
PopularFragmentsAdapter.java
public class PopularFragmentsAdapter extends FragmentStatePagerAdapter {
public PopularFragmentsAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return PopularPostsFragment.newInstance();
case 1:
return PopularUsersFragment.newInstance();
}
return null;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Posts";
case 1:
return "Users";
}
return super.getPageTitle(position);
}
#Override
public int getCount() {
return 2;
}
}
Hope someone finds this useful!
May be my answer is not correct answer for your question. I faced same issue while dealing with Fragments... Below is the mistake I did.
return super.onCreateView(inflater, container, savedInstanceState);
This was creating this kind of issue. I removed this sentence and just inflated some view from the layout files, then it was working fine.
Just check your onCreateView function it will probably solve your problem.
My point is clear here. You should return your inflated view in onCreateView function instead of super.onCreateView(inflater, container, savedInstanceState);
do the binding of pager and tabs in onCreateView. Not in the onActivityCreated.
And you don't need to manually add tabs to TabLayout, it'll do so automatically.
just get the tabs by position and set their text.
binding should be in onCreateView.
Related
I use androidx. In the settings fragment, I want to create the "Preference" buttons and click on them to trigger some individual events.
How can I implement a click listener on a specific Preference?
Thats some my Code:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
public class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preference);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(R.color.graylight));
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle(R.string.action_settings);
toolbar.setLogo(R.drawable.ic_settings_white_24dp);
PreferenceManager preferenceManager = getPreferenceManager();
PreferenceScreen preferenceScreen = getPreferenceScreen();
return view;
}
}
And XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:title="#string/setting_person"
app:initialExpandedChildrenCount="0"
app:key="profile_set">
<Preference
android:id="#+id/preference2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:key="button1"
android:summary="#string/setting_person_data"
android:title="#string/setting_person_reg"
app:icon="#drawable/ic_assignment" />
<Preference
android:key="button"
android:summary="#string/setting_avatar"
android:title="#string/setting_avatar_chg"
app:icon="#drawable/ic_wallpaper_black_24dp" />
</PreferenceCategory>
</PreferenceScreen>
As a result, I want to click on the trigger an event in MainActivity. But this is another question, now at least I should get a listen to the event, for example by calling Toast with the key of the button pressed.
Find the preference, then set a click listener on it.
public class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preference);
Preference preference = findPreference("button1");
preference.setOnClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference p) {
// Handle preference click
}
});
}
}
Please review the documentation on using Preferences:
https://developer.android.com/reference/androidx/preference/package-summary.html
https://developer.android.com/reference/androidx/preference/PreferenceFragmentCompat.html
https://developer.android.com/reference/androidx/preference/PreferenceFragmentCompat.html#findPreference(java.lang.CharSequence)
https://developer.android.com/reference/androidx/preference/Preference.OnPreferenceClickListener.html
Hope that helps!
After much torment, the solution was found as follows: (for AndroidX)
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preference);
Preference preferenceMap = findPreference("button");
preferenceMap.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
getActivity().onBackPressed();
((MainActivity) getActivity()).injectSetting("map");
return true;
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(R.color.graylight));
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle(R.string.action_settings);
toolbar.setLogo(R.drawable.ic_settings_white_24dp);
return view;
}
}
and in XML androidx.preference is added to the element:
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.PreferenceCategory
android:title="#string/setting_person"
app:initialExpandedChildrenCount="1"
app:key="profile_set">
<androidx.preference.Preference
android:id="#+id/preference1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:key="button"
android:summary="#string/setting_person_data"
android:title="#string/setting_person_reg"
app:icon="#drawable/ic_assignment" />
<androidx.preference.Preference
android:id="#+id/preference2"
android:key="button2"
android:summary="#string/setting_avatar"
android:title="#string/setting_avatar_chg"
app:icon="#drawable/ic_wallpaper_black_24dp" />
</androidx.preference.PreferenceCategory>
</androidx.preference.PreferenceScreen>
And do not forget at build.gradle dependencies:
implementation 'androidx.preference:preference:1.1.0'
Maybe someone will need ))
I'm adding a BottomNavigationView to a project, and I would like to have a different text (and icon tint) color for the selected tab (to achieve greying out non-selected tabs effect). I used a color selector resource file with android:state_checked="true" and it worked fine until I implemented the BottomNavigationView.OnNavigationItemSelectedListener to one of my Fragment class. After adding that Listener the tab which was selected by default, remains highlighted even if I select a different tab. I tried different attributes to the state selector but nothing is helping.
Here is how I add the view to my layout:
<android.support.design.widget.BottomNavigationView
android:id="#+id/profileNavBar"
android:layout_alignParentTop="true"
android:background="#color/colorLavander"
app:itemTextColor="#color/nav_item_color"
app:itemIconTint="#color/nav_item_color"
app:menu="#menu/profile_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.BottomNavigationView>
Here is my color selector .xml file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorWhite" android:state_checked="true" />
<item android:color="#color/colorInactive" />
</selector>
And here is my Fragment class:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import org.home.com.gvwrev.R;
public class HomeFragment extends Fragment implements BottomNavigationView.OnNavigationItemSelectedListener {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
BottomNavigationView bottomNavigationView = view.findViewById(R.id.profileNavBar);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
displayRevFragment(new RecordRevFragment());
}
public void displayRevFragment(Fragment fragment) {
getFragmentManager()
.beginTransaction()
.replace(R.id.revenueContainer, fragment)
.commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.itemRecord:
fragment = new RecordRevFragment();
break;
case R.id.itemModify:
fragment = new ModifyRevFragment();
break;
case R.id.itemView:
break;
}
if (fragment != null) {
displayRevFragment(fragment);
}
return false;
}
}
Please help.
The problem is that you should return true when an item is selected in onNavigationItemSelected method. Try this:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.itemRecord:
fragment = new RecordRevFragment();
break;
case R.id.itemModify:
fragment = new ModifyRevFragment();
break;
case R.id.itemView:
break;
}
if (fragment != null) {
displayRevFragment(fragment);
return true;
}
return false;
}
This question already has answers here:
How to set onclick listener for a button in a fragment in android
(3 answers)
Closed 4 years ago.
I want to control button from fragment xml file in the fragment activity. In other activity we can easily control our button by findviewbyid method and after that we can apply setonclicklistener. But here in fragment how do i access the button and apply onclicklistener method.
My Fragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
/**
* A simple {#link Fragment} subclass.
*/
public class QuoteTypeFragment extends Fragment {
public QuoteTypeFragment() {
// Required empty public constructor
}
LinearLayout typeOne, typeTwo, typeThree, typeFour;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vv = inflater.inflate(R.layout.fragment_quote_type, container, false);
return vv;
}
}
My Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#3f525e"
tools:context=".QuoteTypeFragment">
<Button
android:id="#+id/submitbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>
</FrameLayout>
So here I want to control the submit button in fragment.java. How can I acces the button by findviewbyid or findfragmentbyid. And in fragment.java where do I use that code to access submitbutton.
in OnCreateView
Button button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vv = inflater.inflate(R.layout.fragment_quote_type, container, false);
button = vv.findViewById(R.id.submitbutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return vv;
}
In your QuoteTypeFragment.java
1) Cast the widget by using the view. (view.findViewById()).
2) Set the onClickistener.
3) Implement View.OnClickListener and the onClick method.
public class QuoteTypeFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vv = inflater.inflate(R.layout.fragment_quote_type, container, false);
Button mSubmitButton = vv.findViewById(R.id.submitbutton);
mSubmitButton.setonClickListener(this);
return vv;
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submitbutton:
// Control the button
break;
}
}
It is very easy to control button from fragment XML file in the fragment activity.
In your onCreateView use these lines,
Button button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vv = inflater.inflate(R.layout.fragment_quote_type, container, false);
button = vv.view.findViewById(R.id.submitbutton);
return vv;
}
I need your help on this. I am going to make an app using ViewPager and I since I never been programming for android before I thought it would be good to make a sample app first. I want to use the ViewPager a little bit differently than the classic list-of-items-style so I made an app that will show all the colors (or every 10th color) from #000000 to #FFFFFF.
It doesn't work. I've started the app on the emulator but I just get a white screen. If the default position of the ViewPager when starting is 0 then the color should be black. And when I try to make a breakpoint the program doesn't stop, or it's never reaching the point. I'm using eclipse.
These are the files of the project
MainActivity.java
package com.example.colorswipe;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
private ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager=(ViewPager)this.findViewById(R.id.pager);
mPager.setAdapter(new MyAdapter());
}
private class MyAdapter extends PagerAdapter {
#Override
public int getCount() {
return 0xFFFFFF/10;
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ColorView view=new ColorView(container.getContext());
view.setBackgroundColor(android.graphics.Color.parseColor(String.format("#%06X", position*10)));
view.setText(position);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
ColorView.java
package com.example.colorswipe;
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ColorView extends LinearLayout {
private TextView tv;
public ColorView(Context context) {
super(context);
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.setLayoutParams(params);
TextView tv=new TextView(context);
this.tv=tv;
this.addView(tv);
}
public void setText(int position) {
tv.setText(Integer.toString(position).toCharArray(), 0, Integer.toString(position).length());
}
}
EDIT - the view pager is there , but I think you don't resolve the color correctly.
I found the problem. isViewFromObject must be implemented as return arg0==arg1.
I have a problem with btn.setOnClickListener(this). I have a simple project that works good, but when I add an action to the button I get the message:
unfortunatelay (app name) has stopped
When I comment btn.setOnClickListener(this) out the project works without any problem.
Here is my code:
package com.example.hello;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
public static final Context PlaceholderFragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).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.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 implements OnClickListener {
Button btn;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
btn=(Button)rootView.findViewById(R.id.Entrer);
btn.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// here is the problem error: no eclosing istance of the type MainActivityis accessible in scope
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
}
My layout is as follows:
<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.hello.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="190dp"
android:text="#string/hello_world"
android:textSize="16dp" />
<Button
android:id="#+id/Entrer"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp"
android:text="Entrer" />
</RelativeLayout>
I dont know if I have simple things that are not go correctly. I'm new to Android programming. Thank you!
btn is null, so when you set onClickListener you get NullPointerException. It's null because it's located in fragment_main.xml, not activity_main.xml. You should move this code from onCreate()
btn = (Button) findViewById(R.id.Entrer);
btn.setOnClickListener(this);
to PlaceholderFragment() as follows:
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);
btn=(Button) rootView.findViewById(R.id.Entrer);
btn.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivity(intent);
}
});
return rootView;
}
}
btn=(Button)findViewById(R.id.Entrer);
btn.setOnClickListener(this);
Most likely, findViewById above is returning null. btn.setOnClickListener is crashing on a null pointer exception. (Because btn is null).
It is extremely likely, since you are using the default fragment structure created by the Eclipse solution, that the Button is really in the fragment layout file (fragment_main.xml) and not in activity_main.xml. (Hence, the button is not in the Activity view, it's in the fragment view).
If that's the case, then move the above two lines into the onCreateView method of the PlaceholderFragment.