simerpeet jassal simerpeet jassal - 4 months ago 46
Android Question

activity unable to start Fragment and unable to show layout related to this. ending with null pointer

I am very new to android. i am following book but following code unable to start

CrimeFragment()
fragment. nothing appear on screen.
Activity
code is following. file name is CrimeActivity

package com.example.simerpreetjassal.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

public class CrimeActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.framgment_container);
if(fragment==null) {
fragment = new CrimeFragment();
(fm.beginTransaction().add(R.id.framgment_container, fragment)).commit();
}

}
}


layout for upper activity. file name is content_crime

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framgment_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>


Fragment code is following. it is unable to show layout related to this and getting null pointer exception on edit text(mTittleField).Can't find valid reason.how can i initialized mTittleField to get rid from null pointer? file name is CrimeFragment

package com.example.simerpreetjassal.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class CrimeFragment extends Fragment {
private Crime mCrime;
private EditText mTittleField;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCrime = new Crime();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, container, false);
mTittleField = (EditText) v.findViewById((R.id.crime_title));
mTittleField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

}
});
return v;

}
}


Layout related to this:file name is crime_fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/crime_title_hint"
android:id="@+id/crime_title"
/>
</LinearLayout>


Logcat

07-28 11:27:09.056 3083-3083/com.example.simerpreetjassal.criminalintent W/System: ClassLoader referenced unknown path: /data/app/com.example.simerpreetjassal.criminalintent-1/lib/x86
07-28 11:27:09.187 3083-3083/com.example.simerpreetjassal.criminalintent W/System: ClassLoader referenced unknown path: /data/app/com.example.simerpreetjassal.criminalintent-1/lib/x86
07-28 11:27:09.309 3083-3083/com.example.simerpreetjassal.criminalintent D/AndroidRuntime: Shutting down VM
07-28 11:27:09.310 3083-3083/com.example.simerpreetjassal.criminalintent E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.simerpreetjassal.criminalintent, PID: 3083
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simerpreetjassal.criminalintent/com.example.simerpreetjassal.criminalintent.CrimeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference
at com.example.simerpreetjassal.criminalintent.CrimeFragment.onCreateView(CrimeFragment.java:26)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-28 11:27:10.147 3083-3089/com.example.simerpreetjassal.criminalintent W/art: Suspending all threads took: 7.535ms

Answer

The name of the xml layout file is not the same you are referring to in the onCreateView() of your fragment.

Your fragment layout xml file is crime_fragment.xml and the layout you are referring to from your Java code is fragment_crime.xml (through the R class).