xml - My app is terminated when i launch it to my device through android studio -


i created simple app thing print "hello world". when try launch on device in point stops , says "unfortunately ,(the name of app) has stopped. " searched everywhere, couldn't find solution problem.

here activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <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=".mainactivity">  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/hello_world"     android:textsize="36sp"     android:textcolor="@android:color/holo_blue_dark"     android:textstyle="bold"     android:layout_marginleft="12dp"     android:layout_margintop="12dp"     />   </relativelayout> 

androidmanifest.xml(if helpful.) :

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.test">  <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity">     <intent-filter>         <action android:name="android.intent.action.main" />          <category android:name="android.intent.category.launcher" />     </intent-filter> </activity> </application>  </manifest> 

logcat:

process: com.example.android.test, pid: 6266 java.lang.runtimeexception: unable start activity componentinfo{com.example.android.test/com.example.android.test.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:3319) @ android.app.activitythread.handlelaunchactivity(activitythread.java:3415) @ android.app.activitythread.access$1100(activitythread.java:229) @ android.app.activitythread$h.handlemessage(activitythread.java:1821) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:7325) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference @ com.example.android.test.mainactivity.oncreate(mainactivity.java:21) @ android.app.activity.performcreate(activity.java:6904) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1136) @ android.app.activitythread.performlaunchactivity(activitythread.java:3266) @ android.app.activitythread.handlelaunchactivity(activitythread.java:3415) @ android.app.activitythread.access$1100(activitythread.java:229) @ android.app.activitythread$h.handlemessage(activitythread.java:1821) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:7325) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120) 

i hope required me solve problem. if need more declerations please let me know.

edit

here main.activity.java code:

package com.example.android.test;  import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.widget.textview;  public class mainactivity extends appcompatactivity {  // used load 'native-lib' library on application startup. static {     system.loadlibrary("native-lib"); }  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      // example of call native method     //textview tv = (textview) findviewbyid(r.id.sample_text);     //tv.settext(stringfromjni()); }  /**  * native method implemented 'native-lib' native library,  * packaged application.  */ public native string stringfromjni();  public class sampleactivity extends appcompatactivity {      // create variable     textview mtextview;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         // other view stuff          // reference         mtextview = (textview) findviewbyid(r.id.text_view);          // can things textview change text         mtextview.settext("hello world!");      }  } 

}

according logcat output looks trying set text of textview before getting reference it, try this. first make sure add id field xml.

<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textsize="36sp" android:textcolor="@android:color/holo_blue_dark" android:textstyle="bold" android:layout_marginleft="12dp" android:layout_margintop="12dp"  android:"@+id/text_view" /> 

then open mainactivity class , add following class

public class sampleactivity extends appcompatactivity {      // create variable     textview mtextview;      @override      public void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          // other view stuff            // reference           mtextview = (textview) findviewbyid(r.id.text_view);            // can things textview change text          mtextview.settext("hello world!");                }    }


Comments