fragment和Thread的空指针问题

大神们帮忙看看,搞了好久都不知道该怎么改。
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class Fragment_login extends Fragment implements OnClickListener {
String url="http://115.29.203.53:10090/a/login";
Button login_btn;
EditText phone_edit,password_edit;
String phone,password;
String content="phone="+phone+"&password="+password;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.login,null);
login_btn=(Button)view. findViewById(R.id.login_btn);
phone_edit=(EditText) view.findViewById(R.id.phone_edit);
password_edit=(EditText)view.findViewById(R.id.password_edit);
login_btn.setOnClickListener(this);
return view;
}
@Override
public void onClick(View arg0){
// TODO Auto-generated method stub
//new HttpThread(url,content).start();
    new HttpThread(url,content,phone_edit.getText().toString(),
     password_edit.getText().toString()).start();//就是在这句话出现空这指针的问题,去掉两个编辑框内容的调用就可以。
}

}
下面这个是线程
package com.example.task3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gson.Gson;

import android.widget.EditText;

public class HttpThread extends Thread{
String phone;
String url;
String verify;
String password;
String content;
StringBuffer sb;
public HttpThread(){

}
public HttpThread(String url,String content){
this.url=url;
this.content=content;
}
public HttpThread(String url,String content,String phone,String verify,String password){
this.phone=phone;
this.url=url;
this.verify=verify;
this.password=password;
this.content=content;
}
public HttpThread(String url,String content,String phone){
this.content=content;
this.url=url;
this.phone=phone;
}
public HttpThread(String url,String content,String phone,String password){
this.content=content;
this.url=url;
this.phone=phone;
this.password=password;
}
public void doPost(String content){
    try {
URL httpurl=new URL(url);
HttpURLConnection con=(HttpURLConnection) httpurl.openConnection();
con.setRequestMethod("POST");
con.setReadTimeout(5000);
OutputStream out=con.getOutputStream();
String content1="phone="+phone+"&password="+password;
out.write(content.getBytes());
BufferedReader read=new BufferedReader(new InputStreamReader(con.getInputStream()));//代码顺序别搞错
String str;
sb=new StringBuffer();
while((str=read.readLine())!=null){
sb.append(str);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    //new JsonUtil().getJsonToStringArray(sb.toString());
    System.out.println(sb);
    Gson gson=new Gson();
    Messages meg=new Messages();
    meg=gson.fromJson(sb.toString(),Messages.class);
        //System.out.println(new JsonUtil().arr);
    System.out.println("code:"+meg.getCode());
    System.out.println("message:"+meg.getMessage());

}
public void run(){
// TODO Auto-generated method stub
 
super.run();
doPost(content);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值