Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android代码中setOnclickListener处的Nullpointer异常_Android_Button_Nullpointerexception - Fatal编程技术网

Android代码中setOnclickListener处的Nullpointer异常

Android代码中setOnclickListener处的Nullpointer异常,android,button,nullpointerexception,Android,Button,Nullpointerexception,我最近发布了一个问题来解决我遇到的一些运行时错误,而且效果很好。然而,我还有一些,我不知道为什么我会出现一个指向我的setOnClickListener按钮的空指针错误。我不熟悉堆栈溢出,但我仍然无法格式化代码和错误 以下是java文件: import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflate

我最近发布了一个问题来解决我遇到的一些运行时错误,而且效果很好。然而,我还有一些,我不知道为什么我会出现一个指向我的setOnClickListener按钮的空指针错误。我不熟悉堆栈溢出,但我仍然无法格式化代码和错误

以下是java文件:

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class OppositeFragment extends Fragment {

    private static TextView sOppositeView;
    private Button mSubmit;
    private int mRedValue, mGreenValue, mBlueValue;
    private EditText mRedSelect, mGreenSelect, mBlueSelect;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.text_fragment, container, false);
        sOppositeView = (TextView) view.findViewById(R.id.textView1);
        mRedSelect = (EditText)view.findViewById(R.id.redValue);
        mGreenSelect = (EditText)view.findViewById(R.id.greenValue);
        mBlueSelect = (EditText)view.findViewById(R.id.blueValue);
        mSubmit = (Button) view.findViewById(R.id.submitButton);
        mSubmit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    mRedValue = Integer.parseInt(mRedSelect.getText().toString());
                    mGreenValue = Integer.parseInt(mGreenSelect.getText().toString());
                    mBlueValue = Integer.parseInt(mBlueSelect.getText().toString());
                    sOppositeView.setTextColor(Color.rgb(mRedValue, mGreenValue, mBlueValue));

                } catch (NumberFormatException nfe) { // do nothing
                }

            }
        });
        return view;
    }

    public void changeTextProperties(int fontsize, String text)
    {
        sOppositeView.setTextSize(100-fontsize);
        sOppositeView.setText(text);
    }
}
第34行是mSubmit.setOnclickListner行

以下是片段的xml:

<?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" >


    <EditText
        android:id="@+id/blueValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/greenValue"
        android:hint="@string/blue_hint"
        android:ems="10"
        android:inputType="number" />

    <EditText
        android:id="@+id/greenValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/redValue"
        android:ems="10"
        android:hint="@string/green_hint"
        android:inputType="number" />

    <TextView
        android:id="@+id/oppositeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/text_label_two" />

    <EditText
        android:id="@+id/redValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/greenValue"
        android:layout_below="@+id/oppositeTextView"
        android:ems="10"
        android:hint="@string/red_hint"
        android:inputType="number" />

    <Button
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/blueValue"
        android:layout_centerHorizontal="true"
        android:text="@string/color_change_label" />

</RelativeLayout>
这些错误已经困扰我好几个小时了。另外,我也为糟糕的格式表示歉意

此外,以下是主要活动的代码:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;


public class FragmentExampleActivity extends FragmentActivity implements ToolbarFragment.ToolbarListener {

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

    }

    @Override
    public void onButtonClick(int fontsize, String text)
    {
        //instantiates the new fragments as well as start editing the text
        TextFragment textFragment = (TextFragment) getSupportFragmentManager()
                .findFragmentById(R.id.text_fragment);
        textFragment.changeTextProperties(fontsize, text);

        OppositeFragment oppositeFragment=(OppositeFragment) getSupportFragmentManager()
                .findFragmentById(R.id.opposite_fragment);
        oppositeFragment.changeTextProperties(fontsize, text);      
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.fragment_example, menu);
        return true;
    }


}
以及用于此的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentExampleActivity" >

    <!-- Fragment that contains the toolbar -->
    <fragment
        android:id="@+id/toolbar_fragment"
        android:name="meng.jerry.fragmentexample.ToolbarFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        tools:layout="@layout/toolbar_fragment"/>
    <!-- Fragment that displays the text -->
    <fragment
        android:id="@+id/text_fragment"
        android:name="meng.jerry.fragmentexample.TextFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/toolbar_fragment"
        android:layout_marginTop="40dp"
        tools:layout="@layout/text_fragment" />
    <!-- Fragment that displays the text in reverse -->
    <fragment
        android:id="@+id/opposite_fragment"
        android:name="meng.jerry.fragmentexample.OppositeFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/text_fragment"
        android:layout_marginTop="16dp"
        tools:layout="@layout/opposite_fragment" />

</RelativeLayout>

在这里,您对TextView使用了错误的id

 sOppositeView = (TextView) view.findViewById(R.id.textView1);
实际上,您的XML包含这个

android:id="@+id/oppositeTextView"

谢谢你的快速回复!我想这可能会导致错误,但它实际上对我得到的一个没有帮助。请更改您的textview id,然后再试一次。它仍然不起作用吗?很遗憾。不,似乎是别的原因/实际上,这里有两个错误是由以下原因引起的:java.lang.NullPointerException。。。。这个错误将解决我的答案。对于另一个错误,你必须发布FragmentExampleActivity代码。好的,明白了。等一下。
android:id="@+id/oppositeTextView"