这是主要的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
<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>
这是我的第一个应用程序,所以我不太了解android我搜索了很多,但我coudnt修复它,因此我问最好的group.thankyou
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
答案 0 :(得分:0)
您需要使用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");
}
}