java - Why is my view object reference still null (Android dev)? -


fiddling android development. have experience in java, i'm pretty new android dev, please bare me.

if i'm interpreting error message below correctly, "view" object seems null reason. looking online, people had similar problem created reference before setting content view. however, can see code, not case. i'm not sure problem is.

here error message when trying run app (using own phone debugging medium, if matters):

04-24 20:54:03.848: e/androidruntime(13773): fatal exception: main 04-24 20:54:03.848: e/androidruntime(13773): process: com.example.fiddle, pid: 13773 04-24 20:54:03.848: e/androidruntime(13773): java.lang.runtimeexception: unable start activity componentinfo{com.example.fiddle/com.example.fiddle.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.relativelayout.setontouchlistener(android.view.view$ontouchlistener)' on null object reference 04-24 20:54:03.848: e/androidruntime(13773):    @ android.app.activitythread.performlaunchactivity(activitythread.java:3254) 04-24 20:54:03.848: e/androidruntime(13773):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:3350) 04-24 20:54:03.848: e/androidruntime(13773):    @ android.app.activitythread.access$1100(activitythread.java:222) 04-24 20:54:03.848: e/androidruntime(13773):    @ android.app.activitythread$h.handlemessage(activitythread.java:1795) 04-24 20:54:03.848: e/androidruntime(13773):    @ android.os.handler.dispatchmessage(handler.java:102) 04-24 20:54:03.848: e/androidruntime(13773):    @ android.os.looper.loop(looper.java:158) 04-24 20:54:03.848: e/androidruntime(13773):    @ android.app.activitythread.main(activitythread.java:7229) 04-24 20:54:03.848: e/androidruntime(13773):    @ java.lang.reflect.method.invoke(native method) 04-24 20:54:03.848: e/androidruntime(13773):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) 04-24 20:54:03.848: e/androidruntime(13773):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120) 04-24 20:54:03.848: e/androidruntime(13773): caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.relativelayout.setontouchlistener(android.view.view$ontouchlistener)' on null object reference 04-24 20:54:03.848: e/androidruntime(13773):    @ com.example.fiddle.mainactivity.oncreate(mainactivity.java:25) 04-24 20:54:03.848: e/androidruntime(13773):    @ android.app.activity.performcreate(activity.java:6876) 04-24 20:54:03.848: e/androidruntime(13773):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1135) 04-24 20:54:03.848: e/androidruntime(13773):    @ android.app.activitythread.performlaunchactivity(activitythread.java:3207) 04-24 20:54:03.848: e/androidruntime(13773):    ... 9 more 

here mainactivity.java code:

package com.example.fiddle;  import android.annotation.suppresslint; import android.annotation.targetapi; import android.os.build; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuitem; import android.view.motionevent; import android.view.view.ontouchlistener; import android.view.view; import android.widget.relativelayout; import android.widget.toast;  @suppresslint("newapi") public class mainactivity extends actionbaractivity {     @override     protected void oncreate(bundle savedinstancestate) {                 super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          relativelayout view = (relativelayout) findviewbyid(r.layout.activity_main);          view.setontouchlistener (new ontouchlistener() {             public boolean ontouch(view v, motionevent event) {                 toast.maketext(getbasecontext(), string.valueof(event.getx()) + ", " + string.valueof(event.gety()), toast.length_long);                 return true;             }         });     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();         if (id == r.id.action_settings) {             return true;         }         return super.onoptionsitemselected(item);     } } 

and here xml document view:

<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"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:clickable="true"     android:focusable="true"     android:focusableintouchmode="true"     tools:context="com.example.fiddle.mainactivity" >      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/hello_world" />  </relativelayout> 

you should add id relativelayout

    <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"         ...          android:id="@+id/root_relativelayout"         ...        >        ...    </relativelayout> 

then in oncreate method, find relativelayout by

  protected void oncreate(bundle savedinstancestate) {                 ...          //relativelayout view = (relativelayout) findviewbyid(r.layout.activity_main);         relativelayout view = (relativelayout) findviewbyid(r.id.root_relativelayout);         ...   }  

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -