0

I recently posted a question to solve some runtime errors I was getting, and it worked well. However, I still have some more and I have no clue why I have what appears to be a nullpointer error that points to my setOnClickListener for a button. I am new to Stack Overflow and I am still having trouble formatting my code and errors.

Here is the java file:

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

Line 34 is the mSubmit.setOnclickListner line.

Here is the xml for the fragment:

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

Lastly, here is the 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

These errors have been bugging me for hours. Also, I apologize for the poor formatting.

Also, here is the code for the Main Activity:

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


}

And the xml for that:

<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 Answer 1

1

Here, you are using wrong id for TextView

 sOppositeView = (TextView) view.findViewById(R.id.textView1);

actually you XML contains this one

android:id="@+id/oppositeTextView"
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the quick reply! I think that would have caused errors but it actually didn't help the one I was getting.
Change you textview id and then try ..still it's not working ?
Sadly. No. It appears to be something else. :/
See actually here you have two errors Caused by: java.lang.NullPointerException.... this error will solve my answer ..for another error you have to post FragmentExampleActivity code .
Ok, got it. Give me a sec.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.