-3

I am getting a null pointer exception at run time.Below I am posted the codes regarding to that.

StackTrace:

08-06 09:25:35.978: E/AndroidRuntime(3029):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-06 09:29:27.708: E/AndroidRuntime(3084): FATAL EXCEPTION: main
08-06 09:29:27.708: E/AndroidRuntime(3084): Process: com.loco.android, PID: 3084
08-06 09:29:27.708: E/AndroidRuntime(3084): java.lang.NullPointerException
08-06 09:29:27.708: E/AndroidRuntime(3084):     at com.sit.loco.frgment.ContactFragment.onCreateView(ContactFragment.java:60)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.View.measure(View.java:16497)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.View.measure(View.java:16497)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.View.measure(View.java:16497)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.View.measure(View.java:16497)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.View.measure(View.java:16497)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.Choreographer.doFrame(Choreographer.java:544)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.os.Handler.handleCallback(Handler.java:733)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.os.Looper.loop(Looper.java:136)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at java.lang.reflect.Method.invoke(Method.java:515)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-06 09:29:27.708: E/AndroidRuntime(3084):     at dalvik.system.NativeStart.main(Native Method)

ContactFragment.java:

public class ContactFragment extends BaseFragment {

    TextView textView1, textView2, textView3, textView4, textView5, textView6,
            textView7, textView8, textView9;
    EditText editText1, editText2, editText3, editText4;
    Button button1;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.layout_contact, null);



        textView1 = (TextView) view.findViewById(R.id.textView1);
        textView2 = (TextView) view.findViewById(R.id.textView2);
        textView3 = (TextView) view.findViewById(R.id.textView3);
        textView4 = (TextView) view.findViewById(R.id.textView4);
        textView5 = (TextView) view.findViewById(R.id.textView5);
        textView6 = (TextView) view.findViewById(R.id.textView6);
        textView7 = (TextView) view.findViewById(R.id.textView7);
        textView8 = (TextView) view.findViewById(R.id.textView8);
        textView9 = (TextView) view.findViewById(R.id.textView9);

        editText1 = (EditText) view.findViewById(R.id.editText1);
        editText2 = (EditText) view.findViewById(R.id.editText2);
        editText3 = (EditText) view.findViewById(R.id.editText3);
        editText4 = (EditText) view.findViewById(R.id.editText4);

        button1 = (Button) view.findViewById(R.id.button1);


        view.findViewById(R.id.inquiry_send).setOnClickListener(
                new OnClickListener() {  ---->Error occurred at this line belong to the reference id `inquiry.xml`

                    @Override
                    public void onClick(View v) {

                        String inqName = editText1.getText().toString();
                        String inqMail = editText2.getText().toString();
                        String inqSub = editText3.getText().toString();
                        String inqMsg = editText4.getText().toString();

                        // Mail Check
                        boolean isValidMail = validEmail(inqMail);
                        boolean isValid = true;

                        if (inqName.length() <= 0) {
                            editText1.setError("Enter Nmae");
                            isValid = false;
                        }

                        if (!isValidMail) {
                            editText2.setError("Not valid Email");
                            isValid = false;
                        }

                        if (inqSub.length() <= 0) {
                            editText3.setError("Enter Subject");
                            isValid = false;
                        }

                        if (inqMsg.length() <= 0) {
                            editText4.setError("Enter Message");
                            isValid = false;
                        }

                        if (isValid) {
                            sendEnquiry(inqName, inqMail, inqSub, inqMsg);
                        }
                    }

                });

        String someContactNum = "0091 8136864010";
        textView8.setText(someContactNum);

        String secondContactNum = "0091 8136894010";
        textView9.setText(secondContactNum);


        return view;
    }

    }

inquiry.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_theme_bg"
    android:orientation="vertical" >
     .
     .
     .
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:scrollbars="none" >
         .
         .
         .
 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp" >
                .
                .
                .
            <TextView
                android:id="@+id/inquiry_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/more_item_click"
                android:padding="10dp"
                android:text="Send"
                android:textColor="#FFFFFF"
                android:textSize="16sp" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

I didn't know how to solve these.Anybody can help me with this.thank you.

Stephen
  • 9,899
  • 16
  • 90
  • 137
  • 2
    i don't know why you post `inquiry.xml` when the layout you are using is called `layout_contact`. – njzk2 Aug 06 '14 at 13:41
  • @njzk2 I am getting an error at this line `view.findViewById(R.id.inquiry_send).setOnClickListener(new OnClickListener()` reference id belong to that – Stephen Aug 06 '14 at 13:42
  • Is it possible that your "inqSub" field is returning a null value somehow? – carterh062 Aug 06 '14 at 13:44
  • yes? you are searching for a view that is not in your layout, I don't understand what you would expect. – njzk2 Aug 06 '14 at 13:49
  • me to didn't know how to solve this. @njzk2 – Stephen Aug 06 '14 at 13:51
  • I don't understand why you are searching for a view that is not in your layout. It is never going to appear on the screen **since it is not in the inflated layout**, so how would you even expect the user to click on it? why don't you simply put the button in the layout you inflate? – njzk2 Aug 06 '14 at 13:53

2 Answers2

1
view.findViewById(R.id.inquiry_send).setOnClickListener...

There is no view with id inquiry_send in the layout your fragment is inflating. Therefore findViewById returns null and setOnClickListener throws a NullPointerException

znat
  • 13,144
  • 17
  • 71
  • 106
0

you are set layout_contact.xml

view = inflater.inflate(R.layout.layout_contact, null);

and access view of inquiry.xml that's why you got Exception

remove

view.findViewById(R.id.inquiry_send).setOnClickListener(
                new OnClickListener() { 
.
.
.});

and you set Click event on Button Like

button1..setOnClickListener(
                new OnClickListener() {
.
.
.
}):
Mr X
  • 1,053
  • 2
  • 11
  • 24