Reputation:
I have few fragments, in my android app, the fragments load fine, but i want to execute an intent by button click(the button is on one of the fragments).Here is the code.
MainActivity.java
public class MainActivity extends FragmentActivity{
ViewPager viewPager=null;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
button = (Button) findViewById(R.id.button);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,eventload.class);
startActivity(myIntent);
}
});
FragmentManager fragmentManager=getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
}
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
if (i==0)
{
fragment = new FragmentA();
}
if (i==1)
{
fragment = new FragmentB();
}
if (i==2)
{
fragment = new FragmentC();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
String title = new String();
if (position==0)
{
return "Technical";
}
if (position==1)
{
return "Cultural";
}
if (position==2)
{
return "Sports";
}
return super.getPageTitle(position);
}
}
Here is the eventload class
public class eventload extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load);
}
}
Load.xml just has a radiobutton.
here is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.layouts"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity"></activity>
<activity android:name=".eventload"></activity>
<activity
android:name=".SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The app always crashes after displaying a splash screen(splash screen is not interfering here). And my logcat says
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layouts/com.example.layouts.MainActivity}: java.lang.NullPointerException
I have mentioned MainActicity in the manifest.
Here is the activity main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/ic_launcher">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
The button is in different fragment frag4.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.layouts.MainActivity$PlaceholderFragment" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded_corner_box" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="See events"
android:id="@+id/button"
android:layout_gravity="left|top" />
</FrameLayout>
</RelativeLayout>
FragmentC.java
public class FragmentC extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag4,container,false);
}
}
Upvotes: 0
Views: 947
Reputation: 3831
You told button is on the fragment but you are instantiating the button in the activity which is not there in the activity. So it is getting NullPointerException. Instantiate the button in the fragment where the button is existed.
UPDATE1 Change your fragment onCreateView() method like this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag4, container, false);
Button button = (Button) view.findViewById(R.id.button);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(getActivity(), eventload.class);
startActivity(myIntent);
}
});
return view
Upvotes: 1
Reputation: 2550
Change the code of frag4 as following:
public class FragmentC extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag4, container, false);
// here v is the View you inflated from frag4.xml
Button button = (Button) v.findViewById(R.id.button);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(getActivity(),eventload.class);
startActivity(myIntent);
}
});
return v;
}
}
Upvotes: 1
Reputation: 7901
Pass getApplicationContext()
as first parameter in when you create intent object.
Now your button.setOnClickListener()
should look like
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(getApplicationContext(),eventload.class);
startActivity(myIntent);
}
});
Upvotes: 0