这个问题很诡异。
声明了一个Button控件
private Button locationButton;在布局文件中已经布局好了。在activity里
locationButton=(Button)findViewById(R.id.location);
这一步不会出错,程序在模拟器上能运行,当然仅仅这一步,点击location按钮没有反应
但是到这一步locationButton.setOnClickListener(this);的时候程序就无法运行了不知道与没有人遇见过这样的问题请求帮助啊。
声明了一个Button控件
private Button locationButton;在布局文件中已经布局好了。在activity里
locationButton=(Button)findViewById(R.id.location);
这一步不会出错,程序在模拟器上能运行,当然仅仅这一步,点击location按钮没有反应
但是到这一步locationButton.setOnClickListener(this);的时候程序就无法运行了不知道与没有人遇见过这样的问题请求帮助啊。
Activity中,你还是实现onclick了?
还有以后最好发个log出来
08-08 09:07:26.213: W/dalvikvm(2653): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-08 09:07:26.223: E/AndroidRuntime(2653): FATAL EXCEPTION: main
08-08 09:07:26.223: E/AndroidRuntime(2653): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wyf.wpf/com.wyf.wpf.ChatAndroidClientActivity}: java.lang.NullPointerException
08-08 09:07:26.223: E/AndroidRuntime(2653): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-08 09:07:26.223: E/AndroidRuntime(2653): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-08 09:07:26.223: E/AndroidRuntime(2653): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-08 09:07:26.223: E/AndroidRuntime(2653): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-08 09:07:26.223: E/AndroidRuntime(2653): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 09:07:26.223: E/AndroidRuntime(2653): at android.os.Looper.loop(Looper.java:123)
08-08 09:07:26.223: E/AndroidRuntime(2653): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-08 09:07:26.223: E/AndroidRuntime(2653): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 09:07:26.223: E/AndroidRuntime(2653): at java.lang.reflect.Method.invoke(Method.java:521)
08-08 09:07:26.223: E/AndroidRuntime(2653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-08 09:07:26.223: E/AndroidRuntime(2653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-08 09:07:26.223: E/AndroidRuntime(2653): at dalvik.system.NativeStart.main(Native Method)
08-08 09:07:26.223: E/AndroidRuntime(2653): Caused by: java.lang.NullPointerException
08-08 09:07:26.223: E/AndroidRuntime(2653): at com.wyf.wpf.ChatAndroidClientActivity.onCreate(ChatAndroidClientActivity.java:67)
08-08 09:07:26.223: E/AndroidRuntime(2653): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-08 09:07:26.223: E/AndroidRuntime(2653): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-08 09:07:26.223: E/AndroidRuntime(2653): ... 11 more
前面已经写了几个button了
试着监听器都没问题,不知道这一个为什么会这样
package com.wyf.wpf;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ChatAndroidClientActivity extends Activity implements OnClickListener, Runnable{
//声明登录名、ip地址、消息对话框、信息
private EditText usernameEdit;
private EditText ipEdit;
private EditText historyEdit;
private EditText messageEdit;
//声明登陆、发送、离开等按钮
private Button loginButton;
private Button sendButton;
private Button leaveButton;
private Button locationButton;
//声明用户名、ip地址、聊天内容、信息输入等字段
private String username,ip,chat_txt,chat_in;
private static final int PORT=8521;
//private double longitude,latitude;
//客户端socket用于向服务器发送信息
Socket socket;
//线程,及时更新信息框中内容
Thread thread;
//声明客户端数据输入输出流
DataInputStream in;
DataOutputStream out;
//是否登陆的标志
boolean flag=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化相关编辑框对象
usernameEdit=(EditText)findViewById(R.id.name);
usernameEdit.setText("pang");
ipEdit=(EditText)findViewById(R.id.ip);
ipEdit.setText("192.168.1.5");
historyEdit=(EditText)findViewById(R.id.history);
messageEdit=(EditText)findViewById(R.id.message);
//初始化按钮并注册相关单击事件
loginButton=(Button)findViewById(R.id.login);
loginButton.setOnClickListener(this);
leaveButton=(Button)findViewById(R.id.leave);
leaveButton.setOnClickListener(this);
sendButton=(Button)findViewById(R.id.send);
//sendButton.setEnabled(false);//设置不可用
sendButton.setOnClickListener(this);
locationButton=(Button)findViewById(R.id.location);
//locationButton.setEnabled(false);
locationButton.setOnClickListener(this);
}
//按钮单击事件处理
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.login://登陆
System.out.println("login");
login();
break;
case R.id.leave://退出
leave();
break;
case R.id.send://发送
send();
break;
/*case R.id.location://定位
location();
break;*/
}
}
//登陆函数
public void login(){
username=usernameEdit.getText().toString();
ip=ipEdit.getText().toString();
//System.out.println("运行到这里");
if(username!=null&&!username.equals("")&&username!="用户输入"&&ip!=null){
try {
//构建客户端Socket对象
//System.out.println("运行到这里");/////////////////////////////////////到这还在运行
socket=new Socket(ip,PORT); ///////////////////////////////////////这一步出错
//System.out.println("运行到这里");
//创建客户端输入输出流,用于对服务器发送或接受数据
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
//得到当前事件
Date now=new Date(System.currentTimeMillis());
System.out.println("运行到这里");/////////////////////////////////////////运行到哲理就断了。
//设置事件显示格式
SimpleDateFormat format=new SimpleDateFormat("hh:mm:ss");
String nowStr=format.format(now);
//服务器端输出数据,一个登陆名只需提示一次登入成功就可以了
if(flag==false){
out.writeUTF("$ "+username+" "+nowStr+"上线了!");
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("登录失败");
}
//开启线程,监听服务器是否有新的消息,如果有则及时更新
//此线程就是这个这个类的线程
thread=new Thread(ChatAndroidClientActivity.this);
thread.start();
//说明已经登陆成功
flag=true;
if(flag){
usernameEdit.setEnabled(false);//设置不能再登陆
loginButton.setEnabled(false);
sendButton.setEnabled(true);//设置发送信息按钮可用
//locationButton.setEnabled(true);
}
}
//判断如果登陆名为空,并且flag=false时则提示如下
else{
if(flag==false){
Toast.makeText(ChatAndroidClientActivity.this,"您还没登陆,请登陆!",Toast.LENGTH_SHORT).show();
}
}
}
//退出聊天系统函数
public void leave(){
//先判断是否是登陆状态,如果是则执行下面,如果不是,则提示
if(flag==true){
if(flag==false){
Toast.makeText(ChatAndroidClientActivity.this,"您还没登陆,请登陆!",Toast.LENGTH_SHORT).show();
return;
}
try{
//设置为空
usernameEdit.setText("");
//提示下线信息
out.writeUTF("<"+username+"> "+"下线了!");
//关闭socket,记住把输入输出流关闭先
out.close();
in.close();
socket.close();
}catch(IOException e){}
}
flag=false;
Toast.makeText(ChatAndroidClientActivity.this,"已退出!",Toast.LENGTH_SHORT).show();
System.exit(0);
ChatAndroidClientActivity.this.finish();
}
//发送聊天信息函数
public void send(){
//如果还没登陆,则提示没登陆信息
if(flag==false){
Toast.makeText(ChatAndroidClientActivity.this,"您还没登陆,请登陆!",Toast.LENGTH_LONG).show();
return;
}
//取得发送聊天信息内容
//chat_txt=messageEdit.getText().toString();
chat_txt="longitude:"+0.0+"latitude:";
//如果为空,则提示
if(chat_txt==null||chat_txt.equals("")){
Toast.makeText(ChatAndroidClientActivity.this,"发送内容不能为空,请重新输入!",Toast.LENGTH_SHORT).show();
}
//如果不为空
else{
//对时间进行相关处理
Date now=new Date(System.currentTimeMillis());
SimpleDateFormat format=new SimpleDateFormat("hh:mm:ss");
String nowStr=format.format(now);
//在history对话框中显示聊天内容
try {
out.writeUTF(username+" "+nowStr+"说:\n"+chat_txt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//清空输入框
messageEdit.setText("");
}
}
//location function
//public void location(){
//LocationManager locationManager=(LocationManager)ChatAndroidClientActivity.this.getSystemService(Context.LOCATION_SERVICE);
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
//}
/*public class TestLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
System.out.println(location.getLongitude());
longitude=location.getAltitude();
System.out.println(location.getLatitude());
//value="hello";
latitude=location.getLatitude();
//latitude=2.4;
} @Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
} @Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
} @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}*/
//客户端启动后的动作
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
//读取服务器发送来得数据信息
chat_in=in.readUTF();
chat_in=chat_in+"\n";
//发送消息,要求刷新界面
myHandler.sendMessage(myHandler.obtainMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*由于Android是现成安全的,不能在非主线程更新界面,因此定义了一个
Handler用于刷新界面*/
Handler myHandler=new Handler(){
public void handleMessage(android.os.Message msg) {
//将信息显示在客户端history的对话框中
historyEdit.append(chat_in);
super.handleMessage(msg);
};
};
//locationButton.setEnabled(false);
locationButton.setOnClickListener(this);locationButton这个对象=null
private Button locationButton=null;
改成这样还是不行
mail.xml里面到底有没有定义那些按扭。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5px"
android:layout_margin="0px"
android:background="@android:drawable/arrow_up_float">
<TextView
android:id="@+id/nameText"
android:layout_width="90px"
android:layout_height="60px"
android:paddingTop="7px"
android:text="用户名:"
android:textColor="@android:color/darker_gray"
android:gravity="right"/>
<EditText
android:id="@+id/name"
android:layout_width="220px"
android:layout_height="60px"
android:layout_toRightOf="@id/nameText"/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="60px"
android:layout_toRightOf="@id/name"
android:text="登陆"/>
<TextView
android:id="@+id/ipText"
android:layout_width="90px"
android:layout_height="60px"
android:gravity="right"
android:layout_below="@id/nameText"
android:layout_alignParentLeft="true"
android:textColor="@android:color/darker_gray"
android:paddingTop="7px"
android:text="IP地址:"/>
<EditText
android:id="@+id/ip"
android:layout_width="220px"
android:layout_height="60px"
android:layout_toRightOf="@id/ipText"
android:layout_alignTop="@id/ipText"
android:digits=".1234567890"
/>
<Button
android:id="@+id/leave"
android:layout_width="wrap_content"
android:layout_height="60px"
android:layout_toRightOf="@id/ip"
android:layout_alignTop="@id/ipText"
android:text="退出"/>
<EditText
android:id="@+id/history"
android:layout_width="fill_parent"
android:layout_height="280px"
android:editable="false"
android:gravity="top"
android:layout_below="@id/ipText"
android:textSize="12px"/>
<EditText
android:id="@+id/message"
android:layout_width="270px"
android:layout_height="80px"
android:layout_below="@id/history"
android:textSize="12px"
android:gravity="top"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_toRightOf="@id/message"
android:layout_alignTop="@id/message"
android:text="发送"/>
<Button
android:name="@+id/location"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_toRightOf="@id/send"
android:layout_alignTop="@id/send"
android:text="位置"/>
</RelativeLayout>
定义了,好蛋疼啊
我就在main。xml里布局了这些控件,在唯一的一个activity里使用了他们,还和别的文件有关系吗
<Button
android:name="@+id/location"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_toRightOf="@id/send"
android:layout_alignTop="@id/send"
android:text="位置"/>
改为<Button
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_toRightOf="@id/send"
android:layout_alignTop="@id/send"
android:text="位置"/>
现在程序改成了这样package com.wyf.wpf;
import java.io.DataInputStream;public class ChatAndroidClientActivity extends Activity implements OnClickListener, Runnable{
//声明登录名、ip地址、消息对话框、信息
private EditText usernameEdit;
private EditText ipEdit;
private EditText historyEdit;
private EditText messageEdit;
//声明登陆、发送、离开等按钮
private Button loginButton;
private Button sendButton;
private Button leaveButton;
private Button locaButton=null;
//声明用户名、ip地址、聊天内容、信息输入等字段
private String username,ip,chat_txt,chat_in;
private static final int PORT=8521;
private double longitude,latitude;
//客户端socket用于向服务器发送信息
Socket socket;
//线程,及时更新信息框中内容
Thread thread;
//声明客户端数据输入输出流
DataInputStream in;
DataOutputStream out;
//是否登陆的标志
boolean flag=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化相关编辑框对象
usernameEdit=(EditText)findViewById(R.id.name);
usernameEdit.setText("pang");
ipEdit=(EditText)findViewById(R.id.ip);
ipEdit.setText("192.168.1.5");
historyEdit=(EditText)findViewById(R.id.history);
messageEdit=(EditText)findViewById(R.id.message);
//初始化按钮并注册相关单击事件
loginButton=(Button)findViewById(R.id.login);
loginButton.setOnClickListener(this);
leaveButton=(Button)findViewById(R.id.leave);
leaveButton.setOnClickListener(this);
sendButton=(Button)findViewById(R.id.send);
sendButton.setEnabled(false);//设置不可用
sendButton.setOnClickListener(this);
locaButton=(Button)findViewById(R.id.location);
//locaButton.setEnabled(false);
locaButton.setOnClickListener(this);
}
//按钮单击事件处理
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.login://登陆
System.out.println("login");
login();
//location();
break;
case R.id.leave://退出
leave();
break;
case R.id.send://发送
//location();
send();
break;
case R.id.location://定位
System.out.println("这里");
//location();
locationthread lt=new locationthread();
lt.start();
break;
}
}
//登陆函数
public void login(){
username=usernameEdit.getText().toString();
ip=ipEdit.getText().toString();
//System.out.println("运行到这里");
if(username!=null&&!username.equals("")&&username!="用户输入"&&ip!=null){
try {
//构建客户端Socket对象
//System.out.println("运行到这里");/////////////////////////////////////到这还在运行
socket=new Socket(ip,PORT); ///////////////////////////////////////这一步出错
//System.out.println("运行到这里");
//创建客户端输入输出流,用于对服务器发送或接受数据
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
//得到当前事件
Date now=new Date(System.currentTimeMillis());
//System.out.println("运行到这里");/////////////////////////////////////////运行到哲理就断了。
//设置事件显示格式
SimpleDateFormat format=new SimpleDateFormat("hh:mm:ss");
String nowStr=format.format(now);
//服务器端输出数据,一个登陆名只需提示一次登入成功就可以了
if(flag==false){
out.writeUTF("$ "+username+" "+nowStr+"上线了!");
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("登录失败");
}
//开启线程,监听服务器是否有新的消息,如果有则及时更新
//此线程就是这个这个类的线程
thread=new Thread(ChatAndroidClientActivity.this);
thread.start();
//说明已经登陆成功
flag=true;
if(flag){
usernameEdit.setEnabled(false);//设置不能再登陆
loginButton.setEnabled(false);
sendButton.setEnabled(true);//设置发送信息按钮可用
locaButton.setEnabled(true);
}
}
//判断如果登陆名为空,并且flag=false时则提示如下
else{
if(flag==false){
Toast.makeText(ChatAndroidClientActivity.this,"您还没登陆,请登陆!",Toast.LENGTH_SHORT).show();
}
}
}
//退出聊天系统函数
public void leave(){
//先判断是否是登陆状态,如果是则执行下面,如果不是,则提示
if(flag==true){
if(flag==false){
Toast.makeText(ChatAndroidClientActivity.this,"您还没登陆,请登陆!",Toast.LENGTH_SHORT).show();
return;
}
try{
//设置为空
usernameEdit.setText("");
//提示下线信息
out.writeUTF("<"+username+"> "+"下线了!");
//关闭socket,记住把输入输出流关闭先
out.close();
in.close();
socket.close();
}catch(IOException e){}
}
flag=false;
Toast.makeText(ChatAndroidClientActivity.this,"已退出!",Toast.LENGTH_SHORT).show();
System.exit(0);
ChatAndroidClientActivity.this.finish();
}
//发送聊天信息函数
public void send(){
//如果还没登陆,则提示没登陆信息
if(flag==false){
Toast.makeText(ChatAndroidClientActivity.this,"您还没登陆,请登陆!",Toast.LENGTH_LONG).show();
return;
}
//取得发送聊天信息内容
//chat_txt=messageEdit.getText().toString();
chat_txt="longitude:"+longitude+"latitude:";
//如果为空,则提示
if(chat_txt==null||chat_txt.equals("")){
Toast.makeText(ChatAndroidClientActivity.this,"发送内容不能为空,请重新输入!",Toast.LENGTH_SHORT).show();
}
//如果不为空
else{
//对时间进行相关处理
Date now=new Date(System.currentTimeMillis());
SimpleDateFormat format=new SimpleDateFormat("hh:mm:ss");
String nowStr=format.format(now);
//在history对话框中显示聊天内容
try {
out.writeUTF(username+" "+nowStr+"说:\n"+chat_txt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//清空输入框
messageEdit.setText("");
}
}
//location function
//public void location(){
//System.out.println("这里1");
/*LocationManager locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
System.out.println("这里2");
/////////这一步有问题。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
System.out.println("这里3");*/
//longitude=0.7;
// }
class locationthread extends Thread{
public void run(){
LocationManager locationManager=(LocationManager)ChatAndroidClientActivity.this.getSystemService(Context.LOCATION_SERVICE);
System.out.println("这里2");
/////////这一步有问题。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
}
}
public class TestLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
System.out.println(location.getLongitude());
longitude=location.getAltitude();
System.out.println(location.getLatitude());
//value="hello";
latitude=location.getLatitude();
//latitude=2.4;
} @Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
} @Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
} @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
//客户端启动后的动作
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
//读取服务器发送来得数据信息
chat_in=in.readUTF();
chat_in=chat_in+"\n";
//发送消息,要求刷新界面
myHandler.sendMessage(myHandler.obtainMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*由于Android是现成安全的,不能在非主线程更新界面,因此定义了一个
Handler用于刷新界面*/
Handler myHandler=new Handler(){
public void handleMessage(android.os.Message msg) {
//将信息显示在客户端history的对话框中
historyEdit.append(chat_in);
super.handleMessage(msg);
};
};
}
程序到了
/////////这一步有问题。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
这一步就出错了,请问是什么问题啊,求教
把你的处理放到try cache中去试试
这是没有找到xml的问题
我的button是在main.xml中定义的
而我先findViewbyId(R.id.button)
然后再setContentView(R.layout.main);
因此找不到main.xml这个文件中的按钮 就空指针了
然后把这两句顺序换过来就没问题了