I have used actionbar and it is working fine. I have four fragment in the actionbar. I want to implement tabhost in one of the fragment. I have used the following code.
package com.main.udebate;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class Info extends Fragment {
public Info() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
private TabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent i = new Intent(Info.this, about.class);
View view = inflater.inflate(R.layout.info, container, false);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup();
TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
tab.setIndicator("my tab content");
tab.setContent(i);
mTabHost.addTab(tab);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
return view;
}
}
I am getting error on Intent i = new Intent(Info.this, about.class); line as The constructor Intent(Info, Class) is undefined.
About.java
package com.main.udebate;
import android.app.Activity;
import android.os.Bundle;
public class about extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
}
Can someone pls help me to set tabhost inside fragment.
Thanks,
try to use this
Intent i = new Intent(getActivity(), about.class);
instead of this line
Intent i = new Intent(Info.this, about.class);
As Info.this does not mean any context thats why you get "The constructor Intent(Info, Class) is undefined." this error.
In fragment where you use this reference you should instead use getActivity() reference
Related
So today I created tab view bar and fragments and added some image buttons to one of the fragments.
When I add android:onClick="name"on the button code and write Intent in .java file, it gives me force close error when I click on that button.
Here's my code:
fragment.xml
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnProgramiPL5x5"
android:minHeight="80dp"
android:background="#mipmap/btn_programi_pl_5x5"
android:onClick="ProgramiPowerlifting5x5"/>
fragment.java
package hr.app.liftme.liftmehr;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
/**
* A simple {#link Fragment} subclass.
*/
public class FragmentPowerlifting extends Fragment {
public FragmentPowerlifting() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_powerlifting, container, false);
}
ProgramiPowerlifting5x5.java
package hr.app.liftme.liftmehr;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class ProgramiPowerlifting5x5 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_programi_powerlifting5x5);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
}
}
Error i get is this:
01-26 23:00:44.766 14491-14491/hr.app.liftme.liftmehr E/AndroidRuntime: FATAL EXCEPTION: main
Process: hr.app.liftme.liftmehr, PID: 14491
java.lang.IllegalStateException: Could not find method ProgramiPowerlifting5x5(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'btnProgramiPL5x5'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
I don't know what is the problem, when I link two activities I use this, name.class, but that didn't worked in this case so i used getActivity and it still does not work.
I would really appreciate help!
Thanks!
The problem here is:
Could not find method ProgramiPowerlifting5x5(View) in a parent or ancestor Context
The onClick attribute refers to the parent Context (as parent Activity) so the fragment's method is not triggered and not handled by this attribute.
You have to handle the click in the Activity:
public class ProgramiPowerlifting5x5 extends AppCompatActivity {
...
// change the method's name to avoid confusion with constuctor
public void ProgramiPowerLifting(View view){
Intent intent = new Intent(this, ProgramiPowerlifting5x5.class);
startActivity(intent);
}
}
Or trigger the same method into your fragment:
public class ProgramiPowerlifting5x5 extends AppCompatActivity {
private ProgramPowerFragment fragment;
...
public void ProgramiPowerLifting(View view){
fragment.ProgramiPowerLifting(view);
}
}
This great answer uses a callback interface which, personally, I'd suggest. Or you can easily forget the onClick xml attribute and do it dynamically with setOnClickListener().
So, let's try the easy way:
public class FragmentPowerlifting extends Fragment {
public FragmentPowerlifting() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// store the layout in a view variable
View v = inflater.inflate(R.layout.fragment_powerlifting, container, false);
// use this view ("v") to retrieve elements on the layout by its id
ImageButton imgButton = (ImageButton) v.findViewById(R.id.btnProgramiPL5x5);
// set a listener to the button
imgButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// open a new activity when onClick is triggered
Intent intent = new Intent(getActivity(), ProgramiPowerLiftingActivity.class);
startActivity(intent);
}
}
// return the inflated view to display it
return v;
}
}
I have created project in Android Studio with Navigation Drawer Activity and I have successfully created it,On navigation drawer activity I have created 3 section, In 1st section I put Button and I want to setOnClickListener method on that button which lead to start new Activity (ex. "xyz.class") I have used code
startActivity(new Intent(this,xyz.class));
but "this" keyword is not working and gives me error.
So, I changed code like
Context c;
startActivity(new Intent(c,xyz.class));
which gives NullPointerException,
My Section1 code is
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.example.pratik.crm50.R;
import com.gc.materialdesign.views.ButtonFloat;
public class Dashboard extends Fragment implements View.OnClickListener {
View rootView;
Context c;
private ButtonFloat float_btn;
private Button but;
private Button btn;
Context c;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View v;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
v = inflater.inflate(R.layout.dashboard_layout,container, false);
} else {
v = super.onCreateView(inflater,container, savedInstanceState);
}
View vv=v.findViewById(R.id.button_simple);
vv.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
// Toast.makeText(c,"Float Button",Toast.LENGTH_SHORT).show();
Log.e("ssas","sasafa");
//startActivity(new Intent(c,xyz.class));
}
}
and I can get successfully log Log.e("ssas","sasafa") on above code.
So how to do this?
Fragment does not extend Context while the Intent constructor expects the first parameter to be exactly that.
The Context c variable is redundant because a Fragment saves the parent activity by default.
You need to use the activity (class that extends Context) that the fragment is attached to:
startActivity(new Intent(getActivity(), xyz.class));
You get a NullPointerException, because you´re never assining a value to Context c.
So as user3249477 also mentioned use the method getActivity() to assign a value to c like c=getActivity();
Or call it directly while starting the Intent. This would look like this: startActivity(new Intent(getActivity(),xyz.class));
I have been fighting with this error for a couple of hours and I am not able to continue. I need your help here!
I am trying to replace my previous TabNavigation at ActionBar by a ViewPager in SDK21, looking at comments within StackOverflow I found this webpage, where the use of PagerTabStrip is described with an example, so I tried to implement it in my activity, however I am getting an strange error.
I tried to google the problem and all the suggestions are not really applicable to my problem (wrong parameters in the constructor are the usual errors) . I also reproduced the error with a simple Activity in order to avoid anything I have in my previous activity, but the error keeps. I attach you here the code I replicated isolated:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TestActivity extends Activity {
CustomPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adding_users_to_list);
mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(), this.getApplicationContext());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
}
class CustomPagerAdapter extends FragmentPagerAdapter {
Context mContext;
public CustomPagerAdapter(FragmentManager fm, Context context) {
super(fm);
mContext = context;
}
#Override
public Fragment getItem(int position) {
// Create fragment object
Fragment fragment = new DemoFragment();
// Attach some data to the fragment
// that we'll use to populate our fragment layouts
Bundle args = new Bundle();
args.putInt("page_position", position + 1);
// Set the arguments on the fragment
// that will be fetched in the
// DemoFragment#onCreateView
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return "Page " + (position + 1);
}
}
class DemoFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout resource that'll be returned
View rootView = inflater.inflate(R.layout.fragment_users, container, false);
// Get the arguments that was supplied when
// the fragment was instantiated in the
// CustomPagerAdapter
Bundle args = getArguments();
((TextView) rootView.findViewById(R.id.text_option)).setText("Page " + args.getInt("page_position"));
return rootView;
}
}
}
I am getting in the call to the constructor the following error:
"The constructor TestActivity.CustomePageAdapter(FragmentManager, Context) is undefined"
In here:
mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(), this.getApplicationContext());
I have tried to introduce the Adapter outside and inside the activity definition and is still not working. Is likely to be something simple, but... I can't see it and I need other eyes. Any idea what I am doing wrong?
change the import of the activity to:
import android.support.v4.app.FragmentActivity;
Also change constructor to:
mCustomPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(), this.getApplicationContext());
The reason is that your activity is used from the ADT:
import android.app.Activity;
and the Fragments used from the support package:
import android.support.v4.app.FragmentManager;
change the import of the activity to:
import android.support.v4.app.FragmentActivity;
I tried to have a view of an ActionBarActivity opening after hitting a spinner button.
There are two items on my spinner, the second one runs fine, but when I tried to access the Categorias item the app throws me a NullPointerException inside the DrawerActivity class. I don't actually know where the problem is. Another ActionBarActivity extension class that I have runs perfectly.
I'm new to Android/Java development.
The Spinner Fragment
import inmostla.ligatangamanga.pruebaintegrar.navigationdrawer.NavigationDrawerActivity;
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.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerFragment extends Fragment {
private static Spinner spinner;
private int selected;
private View mView;
static void setSpinnerContent( View view ){
spinner = (Spinner) view.findViewById(R.id.spinner1);
return ;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_spinner, container, false);
// Now use the above view to populate the spinner.
setSpinnerContent( view );
/**
* Maneja las acciones seleccionadas del Spinner
*/
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
selected = spinner.getSelectedItemPosition();
switch(selected){
case 1:
Intent categorias = new Intent( );
categorias.setClass( getActivity() , NavigationDrawerActivity.class );
startActivity(categorias);
break;
case 2:
Intent convenios = new Intent();
convenios.setClass(getActivity(), ConveniosFragment.class);
startActivity(convenios);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
return view;
}
}
The Navigation Drawer Activity extends a ActionBarActivity
package inmostla.ligatangamanga.pruebaintegrar.navigationdrawer;
import inmostla.ligatangamanga.pruebaintegrar.navigationdrawer.NavigationDrawerFragment;
import inmostla.ligatangamanga.pruebaintegrar.R;
import android.app.Activity;
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.widget.DrawerLayout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class NavigationDrawerActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in
* {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}...
Maybe that's because getActivity() returns null. Try overriding onAttach() and keep Activity reference as a field in your class. Use this reference instead of getActivity() when you needed a reference to context or activity.
I'm wondering how I can get the code below to work with my project as to combine the two so that it could work. What I have is an implemented Navigation Drawer in the app
1st Java Code Using Fragment:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PagesFragment extends Fragment {
public PagesFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
return rootView;
}
}
2nd Java Code Using Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AppointmentActivity extends Activity {
Button sendEmail;
EditText msg;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appointment_layout);
sendEmail = (Button) findViewById(R.id.sndBtn);
sendEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
msg = (EditText) findViewById(R.id.msgTxt);
String message = msg.getText().toString();
sendEmail(message);
}
});
}
protected void sendEmail(String message) {
String[] to=new String[]{"Shop#email.com"};
String subject=("Shop Appointment");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Gmail"));
}
}
I've tried to combine the two myself but I don't have a lot of experience dealing with android to know how to make both of these codes work side by side and not give me a force close. Anything would be helpful!
To combine these codes, you need to declare your Activity like a FragmentActivity (it will be "host" your Fragment). See this answer: https://stackoverflow.com/a/10609839/2668136
And Google training Fragment
Also Google documentation FragmentActivity
Hope this help.