Momo1199 2013-08-21 07:45 采纳率: 0%
浏览 8870
已采纳

在TextView使用setText发生错误

public class MainActivity extends Activity {
private TextView tx1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tx1= (TextView) findViewById(R.id.txtexample);
    try{
    tx1.setText("Hello testing");
    Log.i("Correct","No error");
    }
    catch(Exception e){

        Log.i("Error","Error here");
        e.printStackTrace();
    }
}

不知道为什么这段代码会出错?
堆栈跟踪:

08-21 06:19:43.713: E/Trace(3883): error opening trace file: No such file or directory (2)
08-21 06:19:43.892: I/Error(3883): Error here
08-21 06:19:43.902: W/System.err(3883): java.lang.NullPointerException
08-21 06:19:43.902: W/System.err(3883):     at com.example.textview.MainActivity.onCreate(MainActivity.java:18)
08-21 06:19:43.902: W/System.err(3883):     at android.app.Activity.performCreate(Activity.java:5104)
08-21 06:19:43.902: W/System.err(3883):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-21 06:19:43.902: W/System.err(3883):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-21 06:19:43.913: W/System.err(3883):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-21 06:19:43.913: W/System.err(3883):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-21 06:19:43.913: W/System.err(3883):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-21 06:19:43.923: W/System.err(3883):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 06:19:43.923: W/System.err(3883):     at android.os.Looper.loop(Looper.java:137)
08-21 06:19:43.923: W/System.err(3883):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-21 06:19:43.923: W/System.err(3883):     at java.lang.reflect.Method.invokeNative(Native Method)
08-21 06:19:43.923: W/System.err(3883):     at java.lang.reflect.Method.invoke(Method.java:511)
08-21 06:19:43.933: W/System.err(3883):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-21 06:19:43.933: W/System.err(3883):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-21 06:19:43.943: W/System.err(3883):     at dalvik.system.NativeStart.main(Native Method)
08-21 06:19:44.103: D/(3883): HostConnection::get() New Host Connection established 0x2a15efa0, tid 3883

activity_main.xml

<TextView android:name="@+id/txtexample" 
 android:layout_width="wrap_content" 
  android:layout_height="wrap_content" />
  • 写回答

2条回答

  • JaveZh 2013-08-21 08:10
    关注
    android:name="@+id/txtexample" 
    

    改成

    android:id="@+id/txtexample"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用