ViewPager上的按钮返回NullPointer

时间:2016-08-31 10:42:17

标签: android android-viewpager

  

引起:java.lang.NullPointerException:尝试调用虚方法' void android.widget.Button.setVisibility(int)'在null对象引用上                                                                       在com.fet.minebeta.MainActivity.onClick(MainActivity.java:39)

我已经ViewPager显示多个视图,其中有单个Fragment,但是多个布局,但每个布局的视图具有相同的ID(即TextViews具有相同的id,按钮具有相同的id等。)

我已使用FrameLayout覆盖布局中的所有视图,并使用灰色叠加层和按钮。

事实是,当我点击button(每个布局中相同的ID)时,它会运行一个onClick方法,该方法应该会消失View叠加层和按钮,但是当我尝试时,我得到两个NullPointer,看看:

的onClick

    @Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.unlockButton: // If mine purchased successfully, remove overlay and button
            overlay.setVisibility(View.GONE);
            mineUnlockButton.setVisibility(View.GONE);
            System.out.println("### Mine Purchased ###");
            break;
    }
}

MineFragment( main)

public class MineFragment extends Fragment implements View.OnClickListener {
    // Store instance variables
    private String mName;
    private String mMineral;
    private int mUnlockCost;
    private int mDropRate;
    private int mLayout;
    private boolean mIsUnlocked;

private Button mineUnlockButton;
private View overlay;

// newInstance constructor for creating fragment with arguments
public static MineFragment newInstance(String name, String mineral, int  unlockCost, int dropRate, int layout) {
    MineFragment mineFragment = new MineFragment();
    Bundle args = new Bundle();

    args.putString("MINE_NAME", name);
    args.putString("MINE_MINERAL", mineral);
    args.putInt("MINE_COST", unlockCost);
    args.putInt("MINE_RATE", dropRate);
    args.putInt("MINE_LAYOUT", layout);

    mineFragment.setArguments(args);
    return mineFragment;
}

// newInstance constructor for creating fragment with arguments + IsUnlocked
public static MineFragment newInstance(String name, String mineral, int unlockCost, int dropRate, int layout, boolean isUnlocked) {
    MineFragment mineFragment = new MineFragment();
    Bundle args = new Bundle();

    args.putString("MINE_NAME", name);
    args.putString("MINE_MINERAL", mineral);
    args.putInt("MINE_COST", unlockCost);
    args.putInt("MINE_RATE", dropRate);
    args.putInt("MINE_LAYOUT", layout);
    args.putBoolean("MINE_IS_UNLOCKED", isUnlocked);

    mineFragment.setArguments(args);
    return mineFragment;
}

// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mName = getArguments().getString("MINE_NAME");
    mMineral = getArguments().getString("MINE_MINERAL");
    mUnlockCost = getArguments().getInt("MINE_COST");
    mDropRate = getArguments().getInt("MINE_RATE");
    mLayout = getArguments().getInt("MINE_LAYOUT");
    mIsUnlocked = getArguments().getBoolean("MINE_IS_UNLOCKED", false);
}

// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(mLayout, container, false);

    TextView mineName = (TextView) view.findViewById(R.id.mineName);
    TextView mineCost = (TextView) view.findViewById(R.id.mineCost);
    TextView mineMineral = (TextView) view.findViewById(R.id.mineMineral);
    TextView mineDropRate = (TextView) view.findViewById(R.id.mineDropRate);
    mineUnlockButton = (Button) view.findViewById(R.id.unlockButton);
    overlay = view.findViewById(R.id.grayOverlay);

    mineName.setText(mName);
    mineCost.setText(String.valueOf(mUnlockCost));
    mineMineral.setText(mMineral);
    mineDropRate.setText(String.valueOf(mDropRate));
    mineUnlockButton.setText(String.valueOf(mUnlockCost));

    return view;
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.unlockButton: // If mine purchased successfully, remove overlay and button
            overlay.setVisibility(View.GONE);
            mineUnlockButton.setVisibility(View.GONE);
            System.out.println("### Mine Purchased ###");
            break;
    }
}

}

我已将onClick方法放在MineFragment和MainActivity上,这是MainActivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

FragmentPagerAdapter adapterViewPager;
ViewPager viewPager;

private Button mineUnlockButton;
View overlay;

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

    viewPager = (ViewPager) findViewById(R.id.vpPager);
    adapterViewPager = new MineAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapterViewPager);

    // Initializing
    mineUnlockButton = (Button) findViewById(R.id.unlockButton);
    overlay = findViewById(R.id.grayOverlay);

}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.unlockButton: // If mine purchased successfully, remove overlay and button
            mineUnlockButton.setVisibility(View.GONE);
            System.out.println("### Mine Purchased ###");
            break;
    }
}


}

修改: 整个项目here on GitHub

1 个答案:

答案 0 :(得分:0)

在活动“MainActivity.java”中执行此操作

public void Unlock(View view) // since you are setting onclick from XML
{
   // Do your stuff
}

并从活动中删除View.OnClickListener,这不是必需的。