Friday, June 1, 2018

java - Android - NullPointerException using ViewGroup



When I relocate from one Activity to another using just simple TextView - it works.
piece of code:



 private RMProject currentProject;
@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.open_proj);
Bundle bundle = getIntent().getExtras();
currentProject = bundle.getParcelable("current");
TextView textView = (TextView) findViewById(R.id.label_open_project);
textView.setText("Project "+currentProject.getName());

}



But when I use another activity with the next code in onCreate() method - it doesn't work and I receive NullPointerException. In line #38. I checked, I received textView when used first piece of code.



public class EventsActivity extends Activity {
private RMProject currentProject;
private ListView listView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventDBManager.init(this);
ViewGroup contentView = (ViewGroup) getLayoutInflater().inflate(R.layout.orm_event_list, null);

listView = (ListView) contentView.findViewById(R.id.ormListView);
Bundle bundle = getIntent().getExtras();
currentProject = bundle.getParcelable("current");
TextView textView = (TextView) findViewById(R.id.ormTextView);
//38 line - next, which cause exception
textView.setText("Project "+currentProject.getName());
Button btn = (Button) contentView.findViewById(R.id.ormButtonAdd);
setupButton(btn);
setContentView(contentView);
}

}


I guess that issue with the components layout.
Here is my layout file:




android:layout_width="match_parent"
android:layout_height="match_parent">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:editable="true"
android:text="Project name"
android:id="@+id/ormTextView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="90dp"
android:textSize="20dp"/>

android:id="@+id/ormButtonAdd"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add " />
android:id="@+id/ormListView"
android:layout_above="@id/ormButtonAdd"


android:layout_width="fill_parent"
android:layout_height="fill_parent" />



And exception that I've been received:




04-15 18:26:29.209 1401-1401/nikolay.rm E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: nikolay.rm, PID: 1401

java.lang.RuntimeException: Unable to start activity ComponentInfo{nikolay.rm/nikolay.rm.activities.EventsActivity}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at nikolay.rm.activities.EventsActivity.onCreate(EventsActivity.java:38)

at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)



Answer



Your missing contentView reference in the following line




TextView textView = (TextView) findViewById(R.id.ormTextView);


So fix would be to replace it with



TextView textView = (TextView) contentView .findViewById(R.id.ormTextView);


Hope this helps



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...