Using Intent in an Android application to show another activity

In my Android application, I have two activity classes. I have a button on the first one and I want to show the second when it is clicked, but I get an error. Here are the classes: public class FirstActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button orderButton = (Button)findViewById(; … Read more

Android. Fragment getActivity() sometimes returns null

In developer console error reports sometimes I see reports with NPE issue. I do not understand what is wrong with my code. On emulator and my device application works good without forcecloses, however some users get NullPointerException in fragment class when the getActivity() method is called. Activity pulic class MyActivity extends FragmentActivity{ private ViewPager pager; … Read more

Getting activity from context in android

This one has me stumped. I need to call an activity method from within a custom layout class. The problem with this is that I don’t know how to access the activity from within the layout. ProfileView public class ProfileView extends LinearLayout { TextView profileTitleTextView; ImageView profileScreenImageButton; boolean isEmpty; ProfileData data; String name; public ProfileView(Context … Read more

How to pass data from 2nd activity to 1st activity when pressed back? – android

I’ve 2 activities, Activity1 and Activity2. In Activity1 I’ve a Button and TextView. When the button is clicked Activity2 is started. In Activity2 I’ve an EditText. I want to display the data retrieved from EditText in Activity2 in the TextView in Activity1 when back is pressed from Activity2. can someone help me with the code … Read more

getActivity() returns null in Fragment function

I have a fragment (F1) with a public method like this public void asd() { if (getActivity() == null) { Log.d(“yes”,”it is null”); } } and yes when I call it (from the Activity), it is null… FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction(); F1 f1 = new F1(); transaction1.replace(, f1); transaction1.commit(); f1.asd(); It must be something that … Read more

How to get hosting Activity from a view?

I have an Activity with 3 EditTexts and a custom view which acts a specialised keyboard to add information into the EditTexts. Currently I’m passing the Activity into the view so that I can get the currently focused edit text and update the contents from the custom keyboard. Is there a way of referencing the … Read more