检查布局参考

时间:2017-01-02 11:56:23

标签: java android nullpointerexception

我知道原因可能是我没有引用具有Id的正确布局。但是,我确信他们的ID在相应的布局中。当我开始包含警报对话框时,我收到此错误,这是由inflater引起的吗?感谢这里的帮助。

我想要做的简要概述:我的主要活动是一个有3个片段的viewpager。我主要研究第一个片段,片段主要。在启动时,应用程序将要求新的玩家名称,然后将该数据传输到玩家类。我的按钮只是打开一个对话框,显示与创建的" newplayer"相关的详细信息。希望这会有所帮助。

package fantasy.myapplication;

import android.app.Dialog;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cn.pedant.SweetAlert.SweetAlertDialog;
import fantasy.myapplication.Fragments.fragment_army;
import fantasy.myapplication.Fragments.fragment_main;
import fantasy.myapplication.Fragments.fragment_treasures;
import fantasy.myapplication.player.player;

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;
public player thisplayer;
public String playerID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    final Button btn_playerinfo = (Button) findViewById(R.id.btnplayerid);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar.setTitle("");
    toolbar.setSubtitle("");

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    mViewPager.setCurrentItem(2);

    //Setup Player
    View popup_newplayer = (LayoutInflater.from(MainActivity.this)).inflate(R.layout.popup_newplayer,null);
    AlertDialog.Builder alert_newplayer = new AlertDialog.Builder(MainActivity.this);
    alert_newplayer.setView(popup_newplayer);

    final EditText newplayerid = (EditText) popup_newplayer.findViewById(R.id.tb_newplayer);
    Button btn_newplayer_create = (Button) popup_newplayer.findViewById(R.id.btn_newplayer_ok);
    Button btn_newplayer_cancel = (Button) popup_newplayer.findViewById(R.id.btn_newplayer_cancel);

    final Dialog dialog_newplayer = alert_newplayer.create();
    dialog_newplayer.show();

    btn_newplayer_create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog_newplayer.dismiss();
            playerID = newplayerid.getText().toString();
            thisplayer = new player(playerID);
            btn_playerinfo.setText(newplayerid.getText());
            new SweetAlertDialog(MainActivity.this, SweetAlertDialog.SUCCESS_TYPE)
                    .setTitleText("Success!")
                    .setContentText("New player "+playerID+" created!")
                    .show();

        }
    });

    btn_newplayer_cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog_newplayer.dismiss();
        }
    });

    btn_playerinfo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Display Player Info

            View popup_playerinfo = (LayoutInflater.from(MainActivity.this)).inflate(R.layout.popup_playerinfo,null);
            AlertDialog.Builder alert_playerinfo = new AlertDialog.Builder(MainActivity.this);
            alert_playerinfo.setView(popup_playerinfo);

            TextView playerinfo_id = (TextView) popup_playerinfo.findViewById(R.id.info_playerid);
            playerinfo_id.setText(newplayerid.getText());

            final Dialog dialog_playerinfo = alert_playerinfo.create();
            dialog_playerinfo.show();
        }
    });

}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) { super(fm);}

    @Override
    public Fragment getItem(int position) {
        //return the current tabs
        switch (position) {
            case 0:
                return new fragment_army();
            case 1:
                return new fragment_treasures();
            case 2:
                return new fragment_main();
            default:
                return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Army";
            case 1:
                return "Treasures";
            case 2:
                return "Main";
        }
        return null;
    }

    @Override
    public int getCount() {return 3;}
}

这是我的主要片段XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_bg">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Main page"
    android:id="@+id/textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Player ID"
    android:id="@+id/btnplayerid"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="26dp"
    android:textSize="10dp" />


</RelativeLayout>

这是activity_main XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fantasy.myapplication.MainActivity"
android:id="@+id/main_activity">

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:id="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:layout_alignParentBottom="true">
</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:tabIndicatorColor="#ffffff"
    app:tabGravity="fill"/>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_above="@id/tabs"/>

</RelativeLayout>

My Fragment Class

package fantasy.myapplication.Fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import fantasy.myapplication.R;

public class fragment_main extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    return rootView;
}

}

1 个答案:

答案 0 :(得分:0)

具有位于片段布局中的id = btnplayerid的按钮。而你试图从活动中引用它。这是错误的方式。您无法从活动类内部的片段引用视图 - 它的范围不同。你应该从片段类中引用它。