代码:
package com.android.hello;import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;public class HelloAndroid1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv=new TextView(this);
        tv.setText("Hello World");
        setContentView(tv);       
    }
}
报错:
[2010-10-15 12:41:47 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.Client.sendAndConsume(Client.java:571)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:670)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)[2010-10-15 12:42:26 - DeviceMonitor]Sending jdwp tracking request failed!运行界面可以显示
但没有HelloWorld显示郁闷。

解决方案 »

  1.   

    TextView tv=new TextView(this);

    这句有问题TextView tv= (TextView) this.findViewById(R.id.textView);//textView是你在xml文件定义的TextView的名称 
      

  2.   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.textview); // 设置 Activity 的标题
    setTitle("TextView");

    TextView txt = (TextView) this.findViewById(R.id.textView);
    // 设置文本显示控件的文本内容,需要换行的话就用“\n”
    txt.setText("我是 TextView\n显示文字用的");
    }
      

  3.   

    textView 在哪个xml文件定义的
    是AndroidManifest.xml吗
    我的内容是这样的
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.android.hello"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".HelloAndroid1"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>    </application>
        <uses-sdk android:minSdkVersion="1.0" /><uses-sdk></uses-sdk>
    </manifest> 
    小弟是菜鸟什么都不懂请大家海涵
      

  4.   


    package android.test.hello;import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;public class Hello extends Activity {
        
       
        
            /** Called when the activity is first created. */
            @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TextView tv = new TextView(this);
            tv.setText("Android  helloWorld");
            setContentView(tv);
        }
    }
      

  5.   

    楼主,VIEW配置文件在res的layout里.你是用ADT自动生成的工程吗?
      

  6.   

    是这个文件吧 main.xml<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    </LinearLayout>
      

  7.   

    模拟器启动正常的就是没有显示 tv.setText("Android  helloWorld");里的东西
      

  8.   

    setContentView(R.layout.main);
    具体的界面配置在res/layout/main.xml中进行,好像是hello那个变量的值赋为“hello word ”。我也新手。
      

  9.   

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, Main!</string>
        <string name="app_name">组件测试</string>
    </resources>
    res/values/string.xml文件
      

  10.   

    楼至用代码生成的TextView控件,注意要给控件设上布局参数。最基本的是给他设上大小,width,height,否则是不会显示的,因为他的宽和高都是0,当然什么都看不到。楼至就是缺的这个,至于怎么设置大小,看下api:
    textView.setLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParames.WRAP_CONTENT);
    记不清了,大致应该这样的,主要就是LayoutParams这个类,他枚举了一些列静态变量来做布局控制,
    当然,你也可以用指定像素大小的方法设置控件大小。一句话,还是看API……另外,控件布局,强烈推荐在xml里面配,也就是程序的layout里面些布局文件
      

  11.   

    一般LAYOUT是写成XML文件的,如果你一定要写在代码里,可以看一下这个例子public HelloActivity extends Activity
    {
    public viud onCreate(Bundle bInpt)
    {
    super.onCreate(bInpt);
    TextView txtHello = new TextView(this);
    txtHello.setText("Hello,World");
    txtHello.setTextLine(1);
    txtHello.setTextSize(36);
    txtHello.setVisible(View.VISIBLE);
    txtHello.setEnable(true);
    setContentView(txtHello);
    }
    }
      

  12.   

    在main.xml中加一个android:id="@+id/TextView"
      

  13.   


    linlouyijian说的不错。可能是你没指定LayoutParam参数导致的。
    如果代码控制布局,起码也把TextView放在LinearLayout里面吧
      

  14.   

     public void onCreate(Bundle savedInstanceState) {
         //调用父类当中的onCreate方法
            super.onCreate(savedInstanceState);
            //设置当前的Activity使用main.xml作为布局文件,其中R.layout.main是main.xml文件在R.java文件当中的ID
            setContentView(R.layout.main);
            //在main.xml文件当中所定义的控件,都会在R.java文件当中产生相应的ID,本行代码的作用就是在根据这个ID来取得代表该控件的对象
            TextView myTextView = (TextView)findViewById(R.id.myTextView);
            //这一行的作用和上一行类似,只不过这一次取得的是代表按钮的对象
            Button myButton = (Button)findViewById(R.id.myButton);
            //为TextView控件设置String值
            myTextView.setText("我的第一个TextView");
            //为Button控件设置String值
            myButton.setText("我的第一个Button" + "\n" + "test");
            
        }
      

  15.   

    LinearLayout layout = new LinearLayout(this);
    LinearLayout.layoutparam  param = new LinearLayout.layoutparam(100,100);
    TextView tv = new TextView(this);
    tv.setText("HELLO WORLD");
    layout.addview(tv,param);
    setContentView(layout);
    手写的,楼主试试
      

  16.   

    建议lz先研究下Eclipse给到你的Hello world的代码
      

  17.   

    你新建一个里面默认就是输出hello wrold了啊,新建一个去看看就知道咯
      

  18.   

    还没解决?晕
    看如下过程:
    (1)按ECLIPSE建立好工程
    打开Hello ,代码如下:public class Hello extends Activity {
        
       
        
            /** Called when the activity is first created. */
            @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);//这个main是指的在
        }
    }(2)打开main.xml,(位于res/layout/下)内容如下:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView 
        android:id="@+id/textView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    </LinearLayout>其实,到这里,任何修改也不要做,选择run as -》android application即可出结果。
      

  19.   

    我新建了一个HelloWorld工程,然后Copy进LZ的代码,可以正常显示,
    LZ再新建一个工程,只改onCreate里面的,其他不要动,试试
      

  20.   

    楼主的代码没问题,我这里可以看到“Hello World”
    重新建个工程试试吧。