我有一个子菜单,其中包含3个用户选项。选择时,每个选项都会调用以下3个功能之一:
void viewLast5Packets()
{
userDefinedCount = 4;
prefEditor.putString(lastpacketsPHP, "/* Put PHP file link here */");
prefEditor.commit();
assignInfoToHistoryTextView();
}
void viewLast10Packets()
{
userDefinedCount = 9;
prefEditor.putString(lastpacketsPHP, "/* Put PHP file link here */");
prefEditor.commit();
assignInfoToHistoryTextView();
}
void viewLast20Packets()
{
userDefinedCount = 19;
prefEditor.putString(lastpacketsPHP, "/* Put PHP file link here */");
prefEditor.commit();
assignInfoToHistoryTextView();
}
每个函数都会创建一个名为lastpacketsPHP的共享首选项,并将不同的URL放入首选项中。当调用assignInfoToHistoryTextView时,它调用位于不同类文件中的另一个函数。课程如下所示:
package shc_BalloonSat.namespace;
import java.text.DecimalFormat;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;
import android.widget.Toast;
public class Shc_BalloonSat_Activity extends Activity
{
int historyCountFromUser;
httpAPI api = new httpAPI(this);
kmlAPI kml = new kmlAPI(this);
DecimalFormat df = new DecimalFormat("##.#####");
DecimalFormat decimalf = new DecimalFormat("##.######");
SharedPreferences pref;
Editor prefEditor;
String lastpacketsPHP;
// User to determine how many packet the user would like to see.
int userDefinedCount = 4;
/** Called when the activity is first created.
* @param view */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String returned = null;
prefEditor.putString(lastpacketsPHP, "http://www.wktechnologies.com/shc_android_app/get_last_5_BS_packets.php");
try
{
returned = api.getData();
}
catch (Exception e)
{
e.printStackTrace();
}
TextView infoTV = (TextView)this.findViewById(R.id.info);
infoTV.setText(returned);
assignInfoToInfoTextView();
assignInfoToHistoryTextView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
SubMenu submenu = menu.addSubMenu(0, Menu.FIRST, Menu.NONE, "Preferences");
submenu.add(0, 5, Menu.NONE, "Get Last 5 Packets");
submenu.add(0, 10, Menu.NONE, "Get Last 10 Packets");
submenu.add(0, 20, Menu.NONE, "Get Last 20 Packets");
inflater.inflate(R.menu.mainmenu, submenu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.viewKML:
viewKML();
return true;
case 5:
viewLast5Packets();
return true;
case 10:
viewLast10Packets();
return true;
case 20:
viewLast5Packets();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void assignInfoToInfoTextView()
{
TextView infoTV = (TextView)this.findViewById(shc_BalloonSat.namespace.R.id.info);
try
{
JSONArray jArray = new JSONArray(api.result);
for (int count = 0; count < 2; count++)
{
JSONObject json_data = jArray.getJSONObject(count);
double altitudeData = json_data.getDouble("altitude");
String infoText = "Last Known Altitude: " + df.format(altitudeData) + "\n";
Double speedData = json_data.optDouble("speed");
if (speedData.isNaN())
{
speedData = 0.00;
}
infoText += "Last Known Speed: " + df.format(speedData) + "\n";
double recentLatitudeData = json_data.getDouble("latitude");
infoText += "Last Known Latitude: " + df.format(recentLatitudeData) + "\n";
double recentLongitudeData = json_data.getDouble("longitude");
infoText += "Last Known Longtitude: " + df.format(recentLongitudeData) + "\n";
infoTV.setText(infoText);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
Log.e("<tag>", e.getMessage());
Toast.makeText(this,"Error in (Last Known) method!",Toast.LENGTH_SHORT).show();
}
}
public void assignInfoToHistoryTextView()
{
try
{
JSONArray jArray = new JSONArray(api.result);
for (int count = 1; count <= userDefinedCount; count++)
{
TextView historyTV = (TextView)this.findViewById(shc_BalloonSat.namespace.R.id.history);
JSONObject json_data = jArray.getJSONObject(count);
double altitudeData = json_data.getDouble("altitude");
String historyText = "Altitude: " + decimalf.format(altitudeData) + "\n";
Double speedData = json_data.optDouble("speed");
if (speedData.isNaN())
{
speedData = 0.00;
}
historyText += "Speed: " + df.format(speedData) + "\n";
double latitudeData = json_data.getDouble("latitude");
historyText += "Latitude: " + df.format(latitudeData) + "\n";
double longitudeData = json_data.getDouble("longitude");
historyText += "Longtitude: " + df.format(longitudeData) + "\n\n";
historyTV.append(historyText);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
Log.e("log_tag", "Error parsing data: " + e.toString());
}
}
void viewLast5Packets()
{
userDefinedCount = 4;
prefEditor.putString(lastpacketsPHP, "http://www.wktechnologies.com/shc_android_app/get_last_5_BS_packets.php");
prefEditor.commit();
assignInfoToHistoryTextView();
}
void viewLast10Packets()
{
userDefinedCount = 9;
prefEditor.putString(lastpacketsPHP, "http://www.wktechnologies.com/shc_android_app/get_last_10_BS_packets.php");
prefEditor.commit();
assignInfoToHistoryTextView();
}
void viewLast20Packets()
{
userDefinedCount = 19;
prefEditor.putString(lastpacketsPHP, "http://www.wktechnologies.com/shc_android_app/get_last_20_BS_packets.php");
prefEditor.commit();
assignInfoToHistoryTextView();
}
public void viewKML()
{
kml.openKML();
}
}
在httpPost中,我试图设置它,以便根据用户选择的选项调用任何选择的PHP文件。我这样做是通过插入获取首选项的值并将其放在httpPost中。当我在模拟器中运行应用程序时,它会立即崩溃,直到我将首选项代码放在httpPost中才会发生,因此我知道我的语法错误我只是不知道我做错了什么。任何帮助将不胜感激。
我在运行时使用上面的代码得到这些错误:
02-02 13:42:14.973: E/AndroidRuntime(230): Uncaught handler: thread main exiting due to uncaught exception
02-02 13:42:15.213: D/dalvikvm(230): GC freed 3178 objects / 319424 bytes in 131ms
02-02 13:42:15.283: E/AndroidRuntime(230): java.lang.StackOverflowError
02-02 13:42:15.283: E/AndroidRuntime(230): at java.lang.Thread.currentThread(Thread.java:588)
02-02 13:42:15.283: E/AndroidRuntime(230): at java.lang.ThreadLocal.get(ThreadLocal.java:59)
02-02 13:42:15.283: E/AndroidRuntime(230): at android.os.Looper.myLooper(Looper.java:137)
02-02 13:42:15.283: E/AndroidRuntime(230): at android.os.Handler.<init>(Handler.java:119)
02-02 13:42:15.283: E/AndroidRuntime(230): at android.app.Activity.<init>(Activity.java:673)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:21)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.java:24)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.httpAPI.<init>(httpAPI.java:20)
02-02 13:42:15.283: E/AndroidRuntime(230): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.<init>(Shc_BalloonSat_Activity.
02-02 13:42:15.373: I/dalvikvm(230): threadid=7: reacting to signal 3
02-02 13:42:15.373: E/dalvikvm(230): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
答案 0 :(得分:2)
public class httpAPI
{
Shc_BalloonSat_Activity shc;
...
httppost = new HttpPost(shc.pref.getString(shc.lastpacketsPHP,
shc.lastpacketsPHP.toString()));
最初,问题是shc在没有被初始化的情况下被使用。关注问题,因为HttpPost
对象需要一个SharedPreferences
实例(可以通过上下文访问),我建议在httpAPI
类中添加一个构造函数。例如:
public class httpAPI {
Shc_BalloonSat_Activity shc;
HttpPost httppost;
...
public httpAPI(Shc_BalloonSat_Activity aContext) {
shc = aContext;
}
...
public String getData() throws Exception {
...
httppost = new HttpPost(shc.pref.getString(shc.lastpacketsPHP, shc.lastpacketsPHP.toString());