的setText导致应用程序崩溃 [英] setText causing application to crash
本文介绍了的setText导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的Android的发展。我想不通这是为什么在仿真器崩溃,当我在我的Android模拟器运行这个程序,它会打开然后快速关闭给我一个不幸的Hello World已停止的消息。
公共类HelloAndroidActivity延伸活动{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
TextView的T =(的TextView)findViewById(R.id.textView1);
t.setText(Secoknd要显示的文字!); }
}
XML文件:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =FILL_PARENT
机器人:方向=横向> <的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =match_parent
机器人:layout_height =38dp
机器人:编辑=真
机器人:文字=TextView的/> < / LinearLayout中>
LogCat中:
20 10-20:07:48.784:D / AndroidRuntime(601):关闭VM
10-20 20:07:48.784:W / dalvikvm(601):主题ID = 1:螺纹未捕获的异常(组= 0x409951f8)退出
10-20 20:07:48.804:E / AndroidRuntime(601):致命异常:主要
10-20 20:07:48.804:E / AndroidRuntime(601):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.helloandroid / com.example.helloandroid.HelloAndroidActivity}:显示java.lang.NullPointerException
10-20 20:07:48.804:E / AndroidRuntime(601):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
10-20 20:07:48.804:E / AndroidRuntime(601):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-20 20:07:48.804:E / AndroidRuntime(601):在android.app.ActivityThread.access $ 600(ActivityThread.java:122)
10-20 20:07:48.804:E / AndroidRuntime(601):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1146)
10-20 20:07:48.804:E / AndroidRuntime(601):在android.os.Handler.dispatchMessage(Handler.java:99)
10-20 20:07:48.804:E / AndroidRuntime(601):在android.os.Looper.loop(Looper.java:137)
10-20 20:07:48.804:E / AndroidRuntime(601):在android.app.ActivityThread.main(ActivityThread.java:4340)
10-20 20:07:48.804:E / AndroidRuntime(601):在java.lang.reflect.Method.invokeNative(本机方法)
10-20 20:07:48.804:E / AndroidRuntime(601):在java.lang.reflect.Method.invoke(Method.java:511)
10-20 20:07:48.804:E / AndroidRuntime(601):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
10-20 20:07:48.804:E / AndroidRuntime(601):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-20 20:07:48.804:E / AndroidRuntime(601):在dalvik.system.NativeStart.main(本机方法)
10-20 20:07:48.804:E / AndroidRuntime(601):致:显示java.lang.NullPointerException
10-20 20:07:48.804:E / AndroidRuntime(601):在com.example.helloandroid.HelloAndroidActivity.onCreate(HelloAndroidActivity.java:16)
10-20 20:07:48.804:E / AndroidRuntime(601):在android.app.Activity.performCreate(Activity.java:4465)
10-20 20:07:48.804:E / AndroidRuntime(601):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-20 20:07:48.804:E / AndroidRuntime(601):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-20 20:07:48.804:E / AndroidRuntime(601):11 ...更多
10-20 20:12:48.894:I /流程(601):发送信号。 PID:601 SIG:9
解决方案
如果TextView的是空与你提供的code,你必须设置错误布局ID。您code样品具有的setContentView(R.layout.main);
。有没有可能是你提供的XML是比其他main.xml中的文件?
I'm new to android development. I can't figure out why this is crashing in the emulator, When i run this program on my Android emulator it opens then quickly closes giving me a "Unfortunately hello world has stopped message.
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t=(TextView)findViewById(R.id.textView1);
t.setText("Secoknd text to display!");
}
}
xml file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="38dp"
android:editable="true"
android:text="TextView" />
</LinearLayout>
LogCat:
10-20 20:07:48.784: D/AndroidRuntime(601): Shutting down VM
10-20 20:07:48.784: W/dalvikvm(601): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
10-20 20:07:48.804: E/AndroidRuntime(601): FATAL EXCEPTION: main
10-20 20:07:48.804: E/AndroidRuntime(601): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroidActivity}: java.lang.NullPointerException
10-20 20:07:48.804: E/AndroidRuntime(601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
10-20 20:07:48.804: E/AndroidRuntime(601): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-20 20:07:48.804: E/AndroidRuntime(601): at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-20 20:07:48.804: E/AndroidRuntime(601): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-20 20:07:48.804: E/AndroidRuntime(601): at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 20:07:48.804: E/AndroidRuntime(601): at android.os.Looper.loop(Looper.java:137)
10-20 20:07:48.804: E/AndroidRuntime(601): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-20 20:07:48.804: E/AndroidRuntime(601): at java.lang.reflect.Method.invokeNative(Native Method)
10-20 20:07:48.804: E/AndroidRuntime(601): at java.lang.reflect.Method.invoke(Method.java:511)
10-20 20:07:48.804: E/AndroidRuntime(601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-20 20:07:48.804: E/AndroidRuntime(601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-20 20:07:48.804: E/AndroidRuntime(601): at dalvik.system.NativeStart.main(Native Method)
10-20 20:07:48.804: E/AndroidRuntime(601): Caused by: java.lang.NullPointerException
10-20 20:07:48.804: E/AndroidRuntime(601): at com.example.helloandroid.HelloAndroidActivity.onCreate(HelloAndroidActivity.java:16)
10-20 20:07:48.804: E/AndroidRuntime(601): at android.app.Activity.performCreate(Activity.java:4465)
10-20 20:07:48.804: E/AndroidRuntime(601): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-20 20:07:48.804: E/AndroidRuntime(601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-20 20:07:48.804: E/AndroidRuntime(601): ... 11 more
10-20 20:12:48.894: I/Process(601): Sending signal. PID: 601 SIG: 9
解决方案
If the TextView is null with the code you provided, you must be setting the wrong layout id. Your code sample has setContentView(R.layout.main);
. Is it possible that the xml you provided is from a file other than main.xml?
这篇关于的setText导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文