简体   繁体   English

尝试在textview中显示文本时settext强制关闭

[英]settext force close when trying to display text in textview

This is the Main java file,i was trying to use the settext method but everytime i use it it gives me force close 这是主要的Java文件,我尝试使用settext方法,但是每次使用它时,它都会使我强制关闭

  public class SecondActivity extends Activity {

    TextView myAwesomeTextView;

         @Override
      public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       myAwesomeTextView = (TextView) findViewById(R.id.myAwesomeTextView);
       myAwesomeTextView.setText("My Awesome Text");


    }
   }

XML XML格式

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

<TextView
   android:id="@+id/myAwesomeTextView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="s"
   android:textSize="20sp"
   android:editable="true"/>
</LinearLayout>

this is my first app so i dont know much about android i searched alot but i coudnt fix it hence i asked the best group.thankyou 这是我的第一个应用程序,所以我对Android知之甚少,但我搜索了很多,但我无法修复它,因此我要求最好的group.thankyou

Logcat Logcat

12-13 12:21:37.946  29196-29196/eu.itcsolutions.android.tutorial D/AndroidRuntime﹕ Shutting down VM
12-13 12:21:37.947  29196-29196/eu.itcsolutions.android.tutorial E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: eu.itcsolutions.android.tutorial, PID: 29196
    java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.itcsolutions.android.tutorial/eu.itcsolutions.android.tutorial.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at eu.itcsolutions.android.tutorial.SecondActivity.onCreate(SecondActivity.java:47)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-13 12:21:39.658  29196-29196/eu.itcsolutions.android.tutorial I/Process﹕ Sending signal. PID: 29196 SIG: 9

You need to use setContentView(R.layout.second) instead of setContentView(R.layout.main) . 您需要使用setContentView(R.layout.second)而不是setContentView(R.layout.main)

public class SecondActivity extends Activity {

    TextView myAwesomeTextView;

         @Override
      public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

       myAwesomeTextView = (TextView) findViewById(R.id.myAwesomeTextView);
       myAwesomeTextView.setText("My Awesome Text");


    }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM