使用 SharedPreferences...我这样做对吗?

发布于 2025-01-02 00:49:08 字数 13982 浏览 1 评论 0原文

我有一个子菜单,其中包含 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 放入该首选项中。当调用 allocateInfoToHistoryTextView 时,它会调用位于不同类文件中的另一个函数。该类如下所示:

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

I have a submenu that contains 3 choices for the user. Each option when chosen calls one of the 3 functions below:

    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();
}

Each function creates a shared preference called lastpacketsPHP and puts a different URL into the preference. When assignInfoToHistoryTextView is called it calls another function located in a different class file. The class is shown below:

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();
}
}

In the httpPost I'm trying to set it so that it will call whichever PHP file was chosen depending on what option was chosen by the user. I'm doing this by inserting get the value of the preference and putting that in the httpPost. When I run the app in the emulator it crashes immediately and it didn't happen until I put the preference code in the httpPost so I know my syntax is wrong I just don't know what I'm doing wrong. Any help will be greatly appreciated.

I get these errors at runtime with the code above:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2025-01-09 00:49:08
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());
public class httpAPI
{
    Shc_BalloonSat_Activity shc;

    ...

        httppost = new HttpPost(shc.pref.getString(shc.lastpacketsPHP,
                                shc.lastpacketsPHP.toString()));

Initially, the trouble was that shc was being used without being initialized. Following up with the question, since the HttpPost object needs a SharedPreferences instance (which is accessible via a context), I'd suggest adding a constructor to the httpAPI class. For example:

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文