代码:
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显示郁闷。
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显示郁闷。
解决方案 »
- mapview 交通模式定位偏移
- 急!!!如何画镂空多边形!!
- android游戏特效编辑器
- 什么时候使用Looper.prepare();
- 触摸屏问题 [按键有KEY_DOWN 没有KEY_UP]
- 请教WebService:Android平台下如何自动生成代码,及其异步网络请求
- 如何在刷新Listview之前保存数据
- 怎么让eclipse自动用ndk-build编译jni文件呀
- 高德百度位置服务到底有没有走framework的LMS?,懂android位置服务的兄弟帮帮忙!!!
- 统计arraylist数据遇到的问题
- android布局问题(界面设计)
- 求一个android中,取assets中json数据的小功能 在线等
这句有问题TextView tv= (TextView) this.findViewById(R.id.textView);//textView是你在xml文件定义的TextView的名称
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显示文字用的");
}
是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>
小弟是菜鸟什么都不懂请大家海涵
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);
}
}
<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>
具体的界面配置在res/layout/main.xml中进行,好像是hello那个变量的值赋为“hello word ”。我也新手。
<resources>
<string name="hello">Hello World, Main!</string>
<string name="app_name">组件测试</string>
</resources>
res/values/string.xml文件
textView.setLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParames.WRAP_CONTENT);
记不清了,大致应该这样的,主要就是LayoutParams这个类,他枚举了一些列静态变量来做布局控制,
当然,你也可以用指定像素大小的方法设置控件大小。一句话,还是看API……另外,控件布局,强烈推荐在xml里面配,也就是程序的layout里面些布局文件
{
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);
}
}
linlouyijian说的不错。可能是你没指定LayoutParam参数导致的。
如果代码控制布局,起码也把TextView放在LinearLayout里面吧
//调用父类当中的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");
}
LinearLayout.layoutparam param = new LinearLayout.layoutparam(100,100);
TextView tv = new TextView(this);
tv.setText("HELLO WORLD");
layout.addview(tv,param);
setContentView(layout);
手写的,楼主试试
看如下过程:
(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即可出结果。
LZ再新建一个工程,只改onCreate里面的,其他不要动,试试
重新建个工程试试吧。