2017-01-02 38 views
-1

我知道原因可能是我沒有引用具有Id的正確佈局。但是,我確定他們的Id在相應的佈局中。我收到這個錯誤,因爲我開始包含警報對話框,這是由inflater引起的嗎?感謝這裏的幫助。即使在檢查佈局引用後仍然無法解析NullPointerException

我試圖做什麼的簡要概述:我的主要活動是有3個片段的viewpager。我主要工作在第一個片段,片段main。在發佈時,應用程序將要求一個新的玩家名稱,然後將該數據傳輸給玩家類。我的按鈕只是打開一個對話框,顯示與創建的「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> 

我的片段

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

} 
+0

這是錯誤消息:顯示java.lang.NullPointerException:嘗試上的空對象引用 –

+1

btn_playerinfo.setText調用虛擬方法「無效android.widget.Button.setText(java.lang.CharSequence中)」(newplayerid.getText ());嘗試將此行更改爲btn_playerinfo.setText(newplayerid.getText()。toSting()); –

+0

將'btn_playerinfo'更改爲實例級變量 –

回答

0

按鈕使用id = btnplayerid位於在片段佈局。你試圖從活動中引用它。這是錯誤的方式。你不能從活動類中的片段引用視圖 - 這是不同的範圍。你應該從片段類中引用它。

相關問題