Android代码中setOnclickListener的Nullpointerexception

时间:2014-11-17 03:29:04

标签: android button nullpointerexception

我最近发布了一个问题来解决我遇到的一些运行时错误,并且效果很好。但是,我仍然有一些更多,我不知道为什么我有一个似乎是一个nullpointer错误指向我的setOnClickListener按钮。我是Stack Overflow的新手,我仍然无法格式化代码和错误。

这是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>

最后,这是logcat:

11-16 22:34:02.239: E/AndroidRuntime(1188): FATAL EXCEPTION: main
11-16 22:34:02.239: E/AndroidRuntime(1188): Process: meng.jerry.fragmentexample, PID: 1188
11-16 22:34:02.239: E/AndroidRuntime(1188): java.lang.RuntimeException: Unable to start activity ComponentInfo{meng.jerry.fragmentexample/meng.jerry.fragmentexample.FragmentExampleActivity}: android.view.InflateException: Binary XML file line #28: Error inflating class fragment
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.os.Looper.loop(Looper.java:136)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at java.lang.reflect.Method.invoke(Method.java:515)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at dalvik.system.NativeStart.main(Native Method)
11-16 22:34:02.239: E/AndroidRuntime(1188): Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class fragment
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.app.Activity.setContentView(Activity.java:1929)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at meng.jerry.fragmentexample.FragmentExampleActivity.onCreate(FragmentExampleActivity.java:19)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.app.Activity.performCreate(Activity.java:5231)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-16 22:34:02.239: E/AndroidRuntime(1188):     ... 11 more
11-16 22:34:02.239: E/AndroidRuntime(1188): Caused by: java.lang.NullPointerException
11-16 22:34:02.239: E/AndroidRuntime(1188):     at meng.jerry.fragmentexample.OppositeFragment.onCreateView(OppositeFragment.java:35)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1099)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1201)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:292)
11-16 22:34:02.239: E/AndroidRuntime(1188):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-16 22:34:02.239: E/AndroidRuntime(1188):     ... 21 more

这些错误一直困扰着我好几个小时。此外,我为糟糕的格式道歉。

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

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>

1 个答案:

答案 0 :(得分:1)

在这里,您使用的是TextView

的错误ID
 sOppositeView = (TextView) view.findViewById(R.id.textView1);

实际上你的XML包含这个

android:id="@+id/oppositeTextView"