Java Android应用程序制作2.2版本,但不兼容2.2+;版本
我必须做一个应用程序,它应该在所有2.2+版本中都能工作。我已经在android设备和模拟器上进行了测试。但是,如果我启动我的应用程序,该应用程序只能在少数版本中运行,但不能在更高版本中运行。我的应用程序出现空值错误。我不知道为什么会这样。根据我的假设,“如果我们为较低版本开发的应用程序可以在所有其他较高版本中运行”,但这是行不通的。请帮帮我 下面是我在2.2、2.3和一些更高版本中使用的代码。但不适用于3.2及更高版本。我得到的是空值Java Android应用程序制作2.2版本,但不兼容2.2+;版本,java,android,Java,Android,我必须做一个应用程序,它应该在所有2.2+版本中都能工作。我已经在android设备和模拟器上进行了测试。但是,如果我启动我的应用程序,该应用程序只能在少数版本中运行,但不能在更高版本中运行。我的应用程序出现空值错误。我不知道为什么会这样。根据我的假设,“如果我们为较低版本开发的应用程序可以在所有其他较高版本中运行”,但这是行不通的。请帮帮我 下面是我在2.2、2.3和一些更高版本中使用的代码。但不适用于3.2及更高版本。我得到的是空值 @Override protected void onCr
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_geotracker);
textbox1 = (TextView) findViewById(R.id.textinname1);
textbox2 = (TextView) findViewById(R.id.textinname2);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected())
{
msg = "Please login to continue";
login();
}
else
{
msg = "Sorry , network connection is not available.";
message();
}
}
//Login------------------------------------------------------------------------------------
public void login()
{
final EditText uname = new EditText(this);
final EditText upassword = new EditText(this);
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
{
// DialogInterface called while setting the AlertDialog Buttons
public void onClick(DialogInterface dialog, int which)
{
//Here you can perform functions of Alert Dialog Buttons as shown
switch(which)
{
case DialogInterface.BUTTON_POSITIVE:
if(!uname.getText().toString().equals("") && !upassword.getText().toString().equals(""))
{
session_name = uname.getText().toString();
session_pwd = upassword.getText().toString();
try
{
uid = httppost(uname.getText().toString(), upassword.getText().toString(), "login.php"); //Here Iam getting null value.
Log.i("TAG", ""+uid);
}
catch(JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(uid == null || uid.length() == 6)
{
msg = "Either user name or password or both incorrect.";
login();
}
else
{
Toast.makeText(getApplicationContext(), "Hello " + uname.getText().toString() + ", You have successfully logged in..:)", Toast.LENGTH_LONG).show();
IDList()
}
}
else
{
Toast.makeText(getApplicationContext(), "Sorry, Either user name or password or both incorrect..:(", Toast.LENGTH_LONG).show();
msg = "Both user name and password are mandatory fields.";
login();
}
break;
case DialogInterface.BUTTON_NEGATIVE:
exit();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LinearLayout orientation = new LinearLayout(this);
orientation.setOrientation(1); //1 is for vertical orientation
uname.setHint("Enter name");
upassword.setHint("Enter password");
upassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
orientation.addView(uname);
orientation.addView(upassword);
builder.setView(orientation);
builder.setMessage(msg);
builder.setTitle("Login")
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("Submit", dialogClickListener)
.setNegativeButton("Exit App", dialogClickListener).show();
}
public String httppost(String param1, String param2, String file) throws JSONException
{
try
{
String data = URLEncoder.encode("param1", "UTF-8") + "=" + URLEncoder.encode(param1, "UTF-8");
data += "&" + URLEncoder.encode("param2", "UTF-8") + "=" + URLEncoder.encode(param2, "UTF-8");
// Send data
URL url = new URL("http://192.168.1.12/GeoTracker/android/"+file);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
the_string_response = rd.readLine();
wr.close();
rd.close();
}
catch (Exception e)
{
}
textbox1.setText("");
textbox2.setText("");
return the_string_response;
}
这是我的原木猫
`05-21 19:12:50.610: D/dalvikvm(4100): Late-enabling CheckJNI
05-21 19:12:50.850: E/PhonePolicy(4100): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
05-21 19:12:50.970: D/TextLayoutCache(4100): Using debug level: 0 - Debug Enabled: 0
05-21 19:12:51.160: I/dalvikvm(4100): threadid=1: recursive native library load attempt (/system/lib/libwebcore.so)
05-21 19:12:51.160: D/dalvikvm(4100): No JNI_OnLoad found in /system/lib/libchromium_net.so 0x0, skipping init
05-21 19:12:51.220: D/dalvikvm(4100): GC_CONCURRENT freed 134K, 4% free 5078K/5255K, paused 1ms+1ms
05-21 19:12:51.750: D/libEGL(4100): loaded /system/lib/egl/libEGL_mali.so
05-21 19:12:51.760: D/libEGL(4100): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-21 19:12:51.770: D/libEGL(4100): loaded /system/lib/egl/libGLESv2_mali.so
05-21 19:12:51.820: D/OpenGLRenderer(4100): Enabling debug mode 0
05-21 19:12:59.690: D/dalvikvm(4100): GC_CONCURRENT freed 159K, 5% free 5329K/5575K,
paused 2ms+4ms`
05-21 19:13:11.500: I/TAG(4100): null
请帮帮我,
提前感谢…在API级别足够高的设备上,系统会阻止您在主线程中执行网络操作(http post等)。如果您在日志猫中进一步查找(将其设置为verbose),您可能会在其中找到类似NETWORK\u ON\u MAIN\u THREAD\u EXCEPTION的内容 看起来这正是你正在做的。这在较低的API级别上可以很好地工作,因为平台没有限制它(尽管这仍然是个坏主意)
要修复此问题,只需将网络操作移出主线程。考虑在具有足够高API级别的设备上使用AcyCastase< P>系统防止在主线程中进行网络操作(HTTP POST等)。如果您在日志猫中进一步查找(将其设置为verbose),您可能会在其中找到类似NETWORK\u ON\u MAIN\u THREAD\u EXCEPTION的内容 看起来这正是你正在做的。这在较低的API级别上可以很好地工作,因为平台没有限制它(尽管这仍然是个坏主意)
要修复此问题,只需将网络操作移出主线程。考虑使用AcycTasks是的,AsyncTask在为我工作。是的,这项任务对我很有效。Thnaks FoamyGuy先生:-DS)