Android FragmentActivity setOnClickListener失败
在我的android应用程序中,通过Android FragmentActivity setOnClickListener失败,android,android-viewpager,onclicklistener,android-fragmentactivity,Android,Android Viewpager,Onclicklistener,Android Fragmentactivity,在我的android应用程序中,通过ViewPager,我获得了创建ViewPager的FragmentActivity 在onCreate方法中,我尝试为按钮设置OnClickListener。但编译后我的应用程序崩溃了。如果我删除所有setOnClickListeners,则一切正常 你能帮我吗 public class MainActivity extends FragmentActivity { AdvicePageAdapter pageAdapter; Spinn
ViewPager
,我获得了创建ViewPager
的FragmentActivity
在onCreate
方法中,我尝试为按钮设置OnClickListener
。但编译后我的应用程序崩溃了。如果我删除所有setOnClickListener
s,则一切正常
你能帮我吗
public class MainActivity extends FragmentActivity {
AdvicePageAdapter pageAdapter;
Spinner categoriesSpinner;
Button blueButton, greenButton, pinkButton, greyButton, yellowButton;
private OnClickListener oclBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Fragment> fragments = getFragments();
pageAdapter = new AdvicePageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
blueButton = (Button) findViewById(R.id.blueButton);
greenButton = (Button) findViewById(R.id.greenButton);
pinkButton = (Button) findViewById(R.id.pinkButton);
greyButton = (Button) findViewById(R.id.greyButton);
yellowButton = (Button) findViewById(R.id.yellowButton);
oclBtn = new OnClickListener (){
@Override
public void onClick(View v){
}
};
blueButton.setOnClickListener(oclBtn);
greenButton.setOnClickListener(oclBtn);
pinkButton.setOnClickListener(oclBtn);
greyButton.setOnClickListener(oclBtn);
yellowButton.setOnClickListener(oclBtn);
}
}
主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewAdvice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
碎片文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/grey" >
<TextView
android:id="@+id/categoriesTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft = "15dip"
android:layout_marginTop = "15dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/categoriesTitle"
android:textColor="@color/white"
android:textSize="17sp" />
<TextView
android:id="@+id/setingsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft = "15dip"
android:layout_marginBottom = "15dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/settingsTitle"
android:textColor="@color/white"
android:textSize="35sp"/>
<Spinner
android:id="@+id/categorieSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft = "15dip"
android:layout_marginRight = "15dip"
android:layout_marginTop = "15dip"
android:layout_below="@+id/categoriesTitle"
android:entries="@array/categories_array"
android:background="@color/white"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:textSize="17sp" />
<TextView
android:id="@+id/colorsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/categorieSpinner"
android:layout_below="@+id/categorieSpinner"
android:layout_marginTop="20dp"
android:text="@string/colorsTitle"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white"
android:textSize="17sp" />
<View
android:id="@+id/colorsBG"
android:layout_width="84dip"
android:layout_height="84dip"
android:layout_below="@+id/colorsTitle"
android:layout_marginLeft = "13dip"
android:layout_marginTop = "13dip"
android:background="@color/white" />
<Button
android:id="@+id/blueButton"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_marginLeft = "15dip"
android:layout_marginTop = "15dip"
android:layout_below="@+id/colorsTitle"
android:background="@color/blue"
/>
<Button
android:id="@+id/greenButton"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_marginLeft = "110dip"
android:layout_marginTop = "15dip"
android:layout_below="@+id/colorsTitle"
android:background="@color/green"
/>
<Button
android:id="@+id/pinkButton"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_marginLeft = "205dip"
android:layout_marginTop = "15dip"
android:layout_below="@+id/colorsTitle"
android:background="@color/pink"
/>
<Button
android:id="@+id/greyButton"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_marginLeft = "15dip"
android:layout_marginTop = "110dip"
android:layout_below="@+id/colorsTitle"
android:background="@color/lightGrey"
/>
<Button
android:id="@+id/yellowButton"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_marginLeft = "110dip"
android:layout_marginTop = "110dip"
android:layout_below="@+id/colorsTitle"
android:background="@color/yellow"
/>
</RelativeLayout>
完整活动代码
package com.eugene.greatadvice;
import java.util.ArrayList;
import java.util.List;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Spinner;
public class MainActivity extends FragmentActivity {
AdvicePageAdapter pageAdapter;
Spinner categoriesSpinner;
Button blueButton, greenButton, pinkButton, greyButton, yellowButton;
View colorsBG;
public OnClickListener oclBtn = new OnClickListener()
{
@Override
public void onClick(View v)
{
if(v == blueButton)
{
}
if(v == greenButton)
{
}
if(v == pinkButton)
{
}
if(v == greyButton)
{
}
if(v == yellowButton)
{
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Fragment> fragments = getFragments();
pageAdapter = new AdvicePageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
colorsBG = findViewById(R.id.colorsBG);
//init actions
categoriesSpinner = (Spinner) findViewById(R.id.categorieSpinner);
blueButton = (Button) findViewById(R.id.blueButton);
greenButton = (Button) findViewById(R.id.greenButton);
pinkButton = (Button) findViewById(R.id.pinkButton);
greyButton = (Button) findViewById(R.id.greyButton);
yellowButton = (Button) findViewById(R.id.yellowButton);
blueButton.setOnClickListener(oclBtn);
greenButton.setOnClickListener(oclBtn);
pinkButton.setOnClickListener(oclBtn);
greyButton.setOnClickListener(oclBtn);
yellowButton.setOnClickListener(oclBtn);
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(SettingsFragment.newInstance(""));
fList.add(AdviceFragment.newInstance(""));
return fList;
}
private class AdvicePageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public AdvicePageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
}
package com.eugene.greatadvice;
导入java.util.ArrayList;
导入java.util.List;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.RelativeLayout.LayoutParams;
导入android.widget.Spinner;
公共类MainActivity扩展了FragmentActivity{
AdvicePageAdapter;
纺纱机;
按钮蓝色按钮、绿色按钮、粉色按钮、灰色按钮、黄色按钮;
视图颜色SBG;
public OnClickListener oclBtn=new OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
if(v==蓝色按钮)
{
}
如果(v==绿色按钮)
{
}
if(v==小按钮)
{
}
if(v==greyButton)
{
}
如果(v==黄色按钮)
{
}
}
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List fragments=getFragments();
pageAdapter=新的AdvicePageAdapter(getSupportFragmentManager(),片段);
ViewPager pager=(ViewPager)findViewById(R.id.ViewPager);
寻呼机设置适配器(pageAdapter);
colorsBG=findViewById(R.id.colorsBG);
//初始化操作
categorieSpinner=(微调器)findViewById(R.id.categorieSpinner);
blueButton=(按钮)findViewById(R.id.blueButton);
绿色按钮=(按钮)findViewById(R.id.greenButton);
pinkButton=(按钮)findViewById(R.id.pinkButton);
灰色按钮=(按钮)findViewById(R.id.greyButton);
yellowButton=(按钮)findViewById(R.id.yellowButton);
setOnClickListener(oclBtn);
greenButton.setOnClickListener(oclBtn);
pinkButton.setOnClickListener(oclBtn);
setOnClickListener(oclBtn);
yellowButton.setOnClickListener(oclBtn);
}
私有列表getFragments(){
List fList=new ArrayList();
fList.add(SettingsFragment.newInstance(“”);
fList.add(AdviceFragment.newInstance(“”);
回传;
}
私有类AdvicePageAdapter扩展了FragmentPagerAdapter{
私有列表片段;
public AdvicePageAdapter(碎片管理器fm,列出碎片){
超级(fm);
this.fragments=片段;
}
@凌驾
公共片段getItem(int位置){
返回此.fragments.get(位置);
}
@凌驾
public int getCount(){
返回此.fragments.size();
}
}
}
您的按钮和其他视图正在膨胀到您的片段中,而不是您的活动中,因此findViewById()
正在为这些ID返回null
。如果您想让按钮等出现在活动中,请将这些元素从FragmentFile移动到Activity\u main.xml
。否则,将视图初始化和方法移动到片段类。您的按钮和其他视图正在膨胀到片段中,而不是活动中,因此findViewById()
将为这些ID返回null
。如果您想让按钮等出现在活动中,请将这些元素从FragmentFile移动到Activity\u main.xml
。否则,将视图初始化和方法移动到片段类。您的按钮和其他视图正在膨胀到片段中,而不是活动中,因此findViewById()
将为这些ID返回null
。如果您想让按钮等出现在活动中,请将这些元素从FragmentFile移动到Activity\u main.xml
。否则,将视图初始化和方法移动到片段类。您的按钮和其他视图正在膨胀到片段中,而不是活动中,因此findViewById()
将为这些ID返回null
。如果您想让按钮等出现在活动中,请将这些元素从FragmentFile移动到Activity\u main.xml
。否则,将视图初始化和方法移动到片段类中
但编译后我的应用程序崩溃了
发生NullPointerException
是因为您试图在片段
未处于有效状态的不适当时间访问该片段的视图。这些按钮是片段布局的一部分,该片段仅在onCreate()
回调中实例化,其视图将在方法onCreate()
之后创建,因此尝试在不存在的片段视图中查找这些按钮将使引用为空
你所做的也是错误的,因为这不是处理这种互动的好方法。片段被设计为自包含的项目,可以在需要时插入,从活动
中搜索片段的视图会打破这一点,并将片段与活动
联系起来(更不用说出错的几率很高)。要避免此问题(以及上述问题),请使用inter
package com.eugene.greatadvice;
import java.util.ArrayList;
import java.util.List;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Spinner;
public class MainActivity extends FragmentActivity {
AdvicePageAdapter pageAdapter;
Spinner categoriesSpinner;
Button blueButton, greenButton, pinkButton, greyButton, yellowButton;
View colorsBG;
public OnClickListener oclBtn = new OnClickListener()
{
@Override
public void onClick(View v)
{
if(v == blueButton)
{
}
if(v == greenButton)
{
}
if(v == pinkButton)
{
}
if(v == greyButton)
{
}
if(v == yellowButton)
{
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Fragment> fragments = getFragments();
pageAdapter = new AdvicePageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
colorsBG = findViewById(R.id.colorsBG);
//init actions
categoriesSpinner = (Spinner) findViewById(R.id.categorieSpinner);
blueButton = (Button) findViewById(R.id.blueButton);
greenButton = (Button) findViewById(R.id.greenButton);
pinkButton = (Button) findViewById(R.id.pinkButton);
greyButton = (Button) findViewById(R.id.greyButton);
yellowButton = (Button) findViewById(R.id.yellowButton);
blueButton.setOnClickListener(oclBtn);
greenButton.setOnClickListener(oclBtn);
pinkButton.setOnClickListener(oclBtn);
greyButton.setOnClickListener(oclBtn);
yellowButton.setOnClickListener(oclBtn);
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(SettingsFragment.newInstance(""));
fList.add(AdviceFragment.newInstance(""));
return fList;
}
private class AdvicePageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public AdvicePageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
}
/**
* An interface that will transmit the click events to the Activity
*/
public interface ClickDispatch {
void onClickReceived(View btn);
}
public class MainActivity extends FragmentActivity implements ClickDispatch {
// code...
@Override
public void onClickReceived(View btn) {
// one of the fragment's buttons(btn) was clicked so do stuff
}
ClickDispatch mListener;
// in the fragment class
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// this is where the activity is passed to the fragment, we use this to cast the
// activity to our listener to pass it the click events
mListener = (ClickDispatch) activity;
}
// in the onCreateView method of the fragment:
View inflatedView = inflater.inflate(/*your layout*/);
blueButton = (Button) inflatedView.findViewById(R.id.blueButton);
greenButton = (Button) inflatedView.findViewById(R.id.greenButton);
pinkButton = (Button) inflatedView.findViewById(R.id.pinkButton);
greyButton = (Button) inflatedView.findViewById(R.id.greyButton);
yellowButton = (Button) inflatedView.findViewById(R.id.yellowButton);
OnClickListener oclBtn = new OnClickListener (){
@Override
public void onClick(View v){
// dispatch the click event to the Activity
if (mListener != null) {
mListener.onClickReceived(v);
}
}
};
blueButton.setOnClickListener(oclBtn);
greenButton.setOnClickListener(oclBtn);
pinkButton.setOnClickListener(oclBtn);
greyButton.setOnClickListener(oclBtn);
yellowButton.setOnClickListener(oclBtn);