错误NullPointerException异常:锁== NULL [英] Error NullPointerException: lock == null
问题描述
谁能帮我这个错误,请从MySQL数据库到Android选择值当我运行应用程序并搜索ID,它总是说(无效的IP地址)...
我有一个连接到WampServer数据库和检索返回到Android应用价值我的Web服务器上的PHP文件。
字符型
字符串名称;
InputStream为= NULL;
字符串结果= NULL;
串线;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 最终的EditText E_ID =(EditText上)findViewById(R.id.editText1);
按钮,选择=(按钮)findViewById(R.id.button1);
select.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根 。ID = e_id.getText()的toString();
选择();
}
});
}公共无效选择()
{
ArrayList的<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();nameValuePairs.add(新BasicNameValuePair(ID,ID)); 尝试
{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://10.0.2.2/test/select.php);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
Log.e(通1,连接成功);
}
赶上(例外五)
{
Log.e(故障1,e.toString());
Toast.makeText(getApplicationContext(),无效的IP地址,
Toast.LENGTH_LONG).show();
} 尝试
{
读者的BufferedReader =新的BufferedReader
(新的InputStreamReader(是ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
而((行= reader.readLine())!= NULL)
{
sb.append(行+\\ n);
}
is.close();
结果= sb.toString();
Log.e(传2,连接成功);
}
赶上(例外五)
{
Log.e(故障2,e.toString());
}尝试
{
JSONObject的json_data =新的JSONObject(结果);
名称=(json_data.getString(名字));
Toast.makeText(getBaseContext(),名称:+名称,
Toast.LENGTH_SHORT).show();
}
赶上(例外五)
{
Log.e(故障3,e.toString());
}
}
LogCat中错误:
2月11日至17日:40:58.059:E /失败1(1073):android.os.NetworkOnMainThreadException
2月11日至17日:40:58.079:E /失败2(1073):显示java.lang.NullPointerException:锁== NULL
2月11日至17日:40:58.079:E / 3失败(1073):显示java.lang.NullPointerException
任何帮助AP preciated ...
E /失败1(1073):android.os.NetworkOnMainThreadException
因为你对你的主要活动的线程上运行您的网络连接的呼叫(如的HttpClient
)引发此错误。为了摆脱,你需要使用另一个线程这些调用。最简单的方式这样做将是使用异步任务
Can anyone help me with this error please, Select Values from MySQL Database to Android when i run app and search for ID, It always said (Invalid IP Address)...
I have a PHP file on my web server that connects to a WampServer database and retrieves values that are returned to Android app..
String id
String name;
InputStream is=null;
String result=null;
String line;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText e_id=(EditText) findViewById(R.id.editText1);
Button select=(Button) findViewById(R.id.button1);
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
id=e_id.getText().toString();
select();
}
});
}
public void select()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/test/select.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
name=(json_data.getString("name"));
Toast.makeText(getBaseContext(), "Name : "+name,
Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
LogCat Error:
11-17 02:40:58.059: E/Fail 1(1073): android.os.NetworkOnMainThreadException
11-17 02:40:58.079: E/Fail 2(1073): java.lang.NullPointerException: lock == null
11-17 02:40:58.079: E/Fail 3(1073): java.lang.NullPointerException
any help appreciated...
E/Fail 1(1073): android.os.NetworkOnMainThreadException
This error is raised because you run your network connectivity calls (like httpclient
) on your main Activity's thread. To get rid of that you need to use another thread for these calls. The easy way to do so would be to use the Async Task
这篇关于错误NullPointerException异常:锁== NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!