我遇到的问题是我制作的apk正在开发三星galaxyS1和SE XperiaPlay,但不适用于三星Galaxy S4。当用户按下按钮时,主要在登录屏幕上,如果细节正确或显示警告框,则应将用户移动到下一个屏幕。可以按下该按钮,但不会触发任何操作,如果你愿意,可以使用虚拟按钮。我不知道该怎么做。
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.miniproject"
android:versionCode="8"
android:versionName="1.6" >
<compatible-screens>
<screen
android:screenDensity="480"
android:screenSize="normal" />
</compatible-screens>
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:icon="@drawable/domo"
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:label="@string/app_name"
android:name=".splashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.miniproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".tableScreen">
</activity>
</application>
</manifest>
MainActivity.java
package com.example.miniproject;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button submit;
EditText username;
EditText password;
HttpPost httppost;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username =(EditText) findViewById(R.id.editTextUsername);
password = (EditText) findViewById(R.id.editTextPassword);
submit = (Button) findViewById(R.id.buttonSubmit);
submit.setOnClickListener(this);
}
public void toast()
{
Context context = getApplicationContext();
CharSequence text = "Table loading...";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
public void onClick(View v)
{
if(v.getId() == submit.getId())
{
login();
}
}
private void login()
{
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://www.daydreamsoftware.com/connect.php");
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username",username.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("password",password.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
//connect.php response User Found = true; No Such User Found = false
if(response.equalsIgnoreCase("User Found"))
{
toast();
startActivity(new Intent(MainActivity.this, tableScreen.class));
}
else
{
AlertDialog.Builder alertBox = new AlertDialog.Builder(this);
alertBox.setMessage("Wrong username or password");
alertBox.show();
}
}catch(Exception e)
{
e.printStackTrace();
}
}//end:login()
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/sand"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editTextUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editTextPassword"
android:layout_alignLeft="@+id/editTextPassword"
android:layout_marginBottom="34dp"
android:ems="10" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="@string/stringLogIn" />
</RelativeLayout>
我使用此s4AVD为S4创建了AVD,它与手机有同样的问题。我附加了点击按钮后出现的logCat:
11-17 22:57:45.851: W/System.err(832): android.os.NetworkOnMainThreadException
11-17 22:57:45.851: W/System.err(832): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-17 22:57:45.851: W/System.err(832): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-17 22:57:45.851: W/System.err(832): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-17 22:57:45.851: W/System.err(832): at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-17 22:57:45.861: W/System.err(832): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-17 22:57:45.871: W/System.err(832): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-17 22:57:45.871: W/System.err(832): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-17 22:57:45.871: W/System.err(832): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-17 22:57:45.871: W/System.err(832): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-17 22:57:45.871: W/System.err(832): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-17 22:57:45.871: W/System.err(832): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-17 22:57:45.891: W/System.err(832): at com.example.miniproject.MainActivity.login(MainActivity.java:76)
11-17 22:57:45.891: W/System.err(832): at com.example.miniproject.MainActivity.onClick(MainActivity.java:60)
11-17 22:57:45.891: W/System.err(832): at android.view.View.performClick(View.java:4084)
11-17 22:57:45.902: W/System.err(832): at android.view.View$PerformClick.run(View.java:16966)
11-17 22:57:45.902: W/System.err(832): at android.os.Handler.handleCallback(Handler.java:615)
11-17 22:57:45.902: W/System.err(832): at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 22:57:45.912: W/System.err(832): at android.os.Looper.loop(Looper.java:137)
11-17 22:57:45.922: W/System.err(832): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-17 22:57:45.922: W/System.err(832): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 22:57:45.931: W/System.err(832): at java.lang.reflect.Method.invoke(Method.java:511)
11-17 22:57:45.931: W/System.err(832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-17 22:57:45.931: W/System.err(832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 22:57:45.941: W/System.err(832): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
你显然已经打开了'严格模式' - 在模拟器和实际的S4上。
'严格模式'告诉您,您已经在UI线程上完成了网络访问,您不应该这样做。
使用AsyncTask或可用的几个好的http库之一(如OkHTTP,Retrofit,Volley等)将您的网络访问权移动到后台线程。
答案 1 :(得分:1)
使用onCreate中的以下代码禁用严格模式:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
但不建议这样做。使用AsyncTask接口。
答案 2 :(得分:0)
线程策略绕过适用于avd但不适用于实际的S4。 Bellow是我使用的更新代码。一切都按照它的设想工作。我真的想知道为什么严格模式可以在仿真器上以这种方式绕过而不是在设备上?
package com.example.miniproject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button submit;
EditText username;
EditText password;
HttpPost httppost;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username =(EditText) findViewById(R.id.editTextUsername);
password = (EditText) findViewById(R.id.editTextPassword);
submit = (Button) findViewById(R.id.buttonSubmit);
submit.setOnClickListener(this);
}
public void toast()
{
Context context = getApplicationContext();
CharSequence text = "Table loading...";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
public void onClick(View v)
{
if(v.getId() == submit.getId())
{
BackgroundTask one = new BackgroundTask();
one.execute();
}
}
public class BackgroundTask extends AsyncTask<String,Integer,Boolean>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... params)
{
boolean sucess=false;
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://www.daydreamsoftware.com/connect.php");
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username",username.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("password",password.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
//connect.php response User Found = true; No Such User Found = false
if(response.equalsIgnoreCase("User Found"))
{
sucess=true;
}
}
catch(IOException e)
{
e.printStackTrace();
}
return sucess;
}
protected void onPostExecute(Boolean result)
{
if(result){
toast();
startActivity(new Intent(MainActivity.this, tableScreen.class));
}
if(!result){
AlertDialog.Builder alertBox = new AlertDialog.Builder(MainActivity.this);
alertBox.setMessage("Wrong username or password");
alertBox.show();
}
}
}
}