首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获得NPE (编辑)

获得NPE (编辑)
EN

Stack Overflow用户
提问于 2013-03-24 08:06:04
回答 2查看 74关注 0票数 1

我仍然得到NPE,但现在它来自我的onCreate()。不过,当我移除按钮时,一切正常。

以下是XML(每个表行来自不同的XML):

代码语言:javascript
复制
<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp" >

        <Button
            android:id="@+id/doneP1"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="Done" />

        <Button
            android:id="@+id/resetP1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Reset" />

    </TableRow>

<TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp" >

            <Button
                android:id="@+id/doneP2"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="Done" />

            <Button
                android:id="@+id/resetP2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Reset" />

        </TableRow>

下面是代码:

代码语言:javascript
复制
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tictactoegame);

        /*doneP1 = (Button) this.findViewById(R.id.doneP1);
        doneP2 = (Button) this.findViewById(R.id.doneP2);
        resetP1 = (Button) this.findViewById(R.id.resetP1);
        resetP2 = (Button) this.findViewById(R.id.resetP2);

        checkTurn(); //starts the game

        //buttons
        doneP1.setOnClickListener(this);
        doneP2.setOnClickListener(this);
        resetP1.setOnClickListener(this);
        resetP2.setOnClickListener(this);*/ //commented out 
    }

在onCreate之外

代码语言:javascript
复制
@Override
public void onClick(View v) {
       switch(v.getId()) {
       case R.id.doneP1:
            checkTurn();
           break;
       case R.id.doneP2:
           checkTurn();
           break;
       case R.id.resetP1:
           break;
       case R.id.resetP2:
           break;
       }
}

这是支票

代码语言:javascript
复制
public void checkTurn()
    {
        if(turn == 1)
        {
            changeLayout(turn);
            turn = 2;
        }
        else
        {
            changeLayout(turn);
            turn = 1;
        }
    }

这里是变更(编辑:新的changeLayout,我只是重新初始化方法内的每个按钮,但仍然没有效果,我这样做对吗?)

代码语言:javascript
复制
public void changeLayout(int turn)
{
    FragmentManager fm       = getSupportFragmentManager();
    Fragment        fragment = fm.findFragmentById(R.id.fragment_container);
    FragmentTransaction ft;

    if(turn == 1) //if its player 1's turn
    {  
        doneP1 = (Button) this.findViewById(R.id.doneP1);
        resetP1 = (Button) this.findViewById(R.id.resetP1);
        doneP1.setOnClickListener(this);
        resetP1.setOnClickListener(this);
        if(fragment == null) //checks wether the current framelayout contains a fragment or not
        {   
            ft = fm.beginTransaction();
            ft.add(R.id.fragment_container, new PlayerTurn1());
            ft.commit(); 
            //setPlayer1();
        }
        else
        {
            ft = fm.beginTransaction();
            ft.replace(R.id.fragment_container, new PlayerTurn1());
            ft.commit();

        }
    }
    else
    {   
        doneP2 = (Button) this.findViewById(R.id.doneP2); 
        resetP2 = (Button) this.findViewById(R.id.resetP2);  
        doneP2.setOnClickListener(this);
        resetP2.setOnClickListener(this);
        if(fragment == null)
        {
            ft = fm.beginTransaction();;
            ft.add(R.id.fragment_container, new PlayerTurn2());
            ft.commit(); 
            //setPlayer2();
        }
        else
        {
            ft = fm.beginTransaction();;
            ft.replace(R.id.fragment_container, new PlayerTurn2());
            ft.commit(); 
        }
    }
}

这是一只罗格猫

03-24 07:51:55.954: E/AndroidRuntime(5092):引起: java.lang.NullPointerException 03-24 07:51:55.954: E/AndroidRuntime(5092):at java.lang.NullPointerException

第88行是:

代码语言:javascript
复制
doneP1.setOnClickListener(this);

我已经花了大约4个小时的时间来解决这个问题,但我仍然无法解决一个简单的问题。我的头在流口水,我真的很累,我想在睡觉前解决这个问题,所以我会感激你的快速反应。我清理并重建了我的问题。没什么。一切看起来都很好但不是..。NPE总是要闯进来。

EN

回答 2

Stack Overflow用户

发布于 2013-03-24 08:18:42

如果你有NPE的话

代码语言:javascript
复制
doneP1.setOnClickListener(this);

这意味着doneP1是空的。您是否仅通过打印/loggin doneP1、doneP2、resetP1和resetP2 (当然是在第88行之前)进行检查。例如:

代码语言:javascript
复制
Log.d(TAG, "doneP1 = "+doneP1);
Log.d(TAG, "doneP2 = "+doneP2);
...

我猜所有这些组件都是空的。这意味着您可能加载了错误的布局。或者,您可能从一开始就加载了正确的一个,但是您将其更改为其他内容

代码语言:javascript
复制
public void changeLayout(int turn)

编辑:

您的TableRows在不同的布局中?

代码语言:javascript
复制
"Here are the XML(each table rows are from separate XMLs):"

当然,在加载P1布局时,P2组件将为空,反之亦然。

EDIT2:

在changeLayout中加载引用。因为同时屏幕上只有两个按钮(重置和完成),所以实际上只需要两个引用(即不需要donep1和donep2)。然后在更改布局中更改对正确按钮的引用。因此,您需要在按钮的onClick中对其进行逻辑处理。

票数 0
EN

Stack Overflow用户

发布于 2013-03-24 08:26:18

从表面上看,您可以在onCreate中获得这些视图

代码语言:javascript
复制
  doneP1 = (Button) this.findViewById(R.id.doneP1);
    doneP2 = (Button) this.findViewById(R.id.doneP2);
    resetP1 = (Button) this.findViewById(R.id.resetP1);
    resetP2 = (Button) this.findViewById(R.id.resetP2);

但是,在您更改为转身并更改您的布局之后:

代码语言:javascript
复制
public void checkTurn()
{
    if(turn == 1)
    {
        changeLayout(turn);
        turn = 2;
    }
    else
    {
        changeLayout(turn);
        turn = 1;
    }
}

然后运行这个:

代码语言:javascript
复制
ft.replace(R.id.fragment_container, new PlayerTurn1());

你基本上失去了对这些观点的引用。因为已经不存在了。它们被另一个片段的观点所取代。

编辑:

另一种选择是在布局文件中设置onClick属性:

设置"android:onClick="onDoneButtonOnClick"

在您的活动中创建以下方法:

代码语言:javascript
复制
public void onDoneButtonOnClick (View v)
{
      // do here what every you want with this button.
}

在这种情况下,您必须从活动中删除onClickListener的实现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15596398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档