其他分享
首页 > 其他分享> > android – 滑动布局中的Button SetOnClickListener NullPointerException

android – 滑动布局中的Button SetOnClickListener NullPointerException

作者:互联网

每当我使用setOnClickListener方法运行应用程序时,我都会收到错误.否则它工作正常.你能帮我吗?

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文件

<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文件

<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>

这是我的第一个android项目所以请不要太苛刻.非常感谢您的帮助!非常感谢你!

解决方法:

您需要在相应的片段中编写UI事件,试试这个,

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;
    }
}

标签:android,android-fragments,android-layout,android-intent,android-button
来源: https://codeday.me/bug/20190725/1531978.html