gpt4 book ai didi

android - 滑动布局中的按钮 SetOnClickListener NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:18 25 4
gpt4 key购买 nike

每当我使用 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;
}

}

仪表板.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>

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

最佳答案

您需要在相应的 fragment 中编写您的 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 - 滑动布局中的按钮 SetOnClickListener NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20902553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com