fragment和Thread的空指针问题
来源:互联网 发布:中国气象数据网 编辑:程序博客网 时间:2024/05/16 18:08
大神们帮忙看看,搞了好久都不知道该怎么改。
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);
}
}
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
- fragment和Thread的空指针问题
- FragmentPagerAdapter调用Fragment的空指针问题
- fragment中的空指针问题
- Fragment 控件空指针问题
- 关于fragment 的findFragmentById出现空指针问题
- fragment的handler中getActivity空指针问题
- fragment里getactivity空指针问题
- activity+fragment模式下,fragment中Toast频繁空指针异常的问题
- 对空指针赋值的问题 指针的指针 指针的指针和指针数组
- Fragment中getActivity()和getContext()为空的问题
- fragment getActivity()空指针
- Fragment getActivity()空指针?
- Android MVVM DataBinding在Fragment onCreateView()中getAdapter()显示空指针的问题的一种解决方法
- 关于Fragment里面ListView.setAdapter(SimpleAdapter)报空指针的问题
- HibernateTemplate空指针的问题
- Android解决多个Fragment切换时布局重新实例化问题和getActivity空指针问题
- Fragment的getView()为空问题
- Fragment Toast空指针NullPointerException
- 洛谷P3378 手写堆模板
- asp utf-8编码 乱码问题
- 内存
- 变量,常量,静态变量存储的位置
- asp.net 用sqldatareader读取数据
- fragment和Thread的空指针问题
- 汇编语言Assembly(二)
- ZCMU-1375-阶乘的0
- Flash代码在ie正常显示,在Google浏览器错位,火狐浏览不能显示此页面上的内容需要较新版本的 Adobe Flash Player。
- iOS 结构体
- Android与PC通信,客户端运行失败
- 请教:Android下怎样让Activity的一部分用于画图而不占用整个Activity 另一部分显示其他控件
- 安卓编程如何实现 卸载残留文件
- 有没有哪个网站是使用xml编写的?