3

I get an error whenever I run the app with the setOnClickListener method. Otherwise it works fine. Can you guys please help me?

package com.grozav.meetmeup;

import com.grozav.meetmeup.R;
import com.grozav.meetmeup.library.UserFunctions;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;

public class DashboardActivity extends FragmentActivity implements TabListener {

UserFunctions userFunctions;

ViewPager viewPager;
ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

    userFunctions = new UserFunctions();
    if (!userFunctions.isUserLoggedIn(getApplicationContext())) {
        // user is not logged in show login screen
        Intent login = new Intent(getApplicationContext(),
                LoginActivity.class);
        login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(login);
        // Closing dashboard screen
        finish();
    }

    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    viewPager.setOnPageChangeListener(new OnPageChangeListener() {

        public void onPageSelected(int arg0) {
            actionBar.setSelectedNavigationItem(arg0);
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tabA = actionBar.newTab();
    tabA.setText("My Profile");
    tabA.setTabListener(this);

    ActionBar.Tab tabB = actionBar.newTab();
    tabB.setText("Meet Me Up");
    tabB.setTabListener(this);

    actionBar.addTab(tabA);
    actionBar.addTab(tabB);

    /*********************************
            I GET THE ERROR HERE
            *********************************/
    Button logout = (Button) findViewById(R.id.btnLogout);
    logout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(getApplicationContext(), LoginActivity.class); // fix View.getContext() to getContext()
            startActivity(myIntent);    // change to startActivity
        }
    });



}

public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
    viewPager.setCurrentItem(arg0.getPosition());

}

public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

}

class MyAdapter extends FragmentPagerAdapter {

public MyAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {
    Fragment fragment = null;
    if (arg0 == 0) {
        fragment = new FragmentA();
    }
    if (arg0 == 1) {
        fragment = new FragmentB();
    }
    return fragment;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 2;
}

}

dashboard.xml file

<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=".DashboardActivity" />

fragment_a.xml file

<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: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=".FragmentA" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Logout" />

</LinearLayout>

</FrameLayout>

This is my first android project so please don't be too harsh. Your help is greatly appreciated! Thank you very much!

2
  • 1
    (Button) findViewById(R.id.btnLogout); is returning null. make sure the button with id R.id.btnLogout is a part of your R.layout.dashboard xml layout.
    – Gopal Gopi
    Jan 3, 2014 at 11:28
  • Hi Gopal. It's a part of the fragment_a xml, can you help me by telling me how to set the view to the selected fragment? Jan 3, 2014 at 11:30

2 Answers 2

8

You need to write your UI events within according fragment, try this,

public class FragmentA extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_a.xml, null);
        Button logout = (Button)view.findViewById(R.id.btnLogout);
        logout.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
              Intent myIntent = new Intent(getActivity(), LoginActivity.class); 
              startActivity(myIntent);    // change to startActivity
           }
        });
        return view;
    }
}
0
0

try this..

logout.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(getApplicationContext(), LoginActivity.class); // fix View.getContext() to getContext()
                startActivity(myIntent);    /
            }});

If you still face problem then change the button id in XML and try again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.