0

再试一次

我有这个代码示例,它不完整,但它应该让我的观点跨越

 public class MainActivity extends Activity {

    ListView list1;
    LazyAdapter adapter;
    String nUdid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        final Context context = this;
        setContentView(R.layout.main);
        TextView viewtitle1 = (TextView) findViewById(R.id.textView1);

        JSONObject j = new JSONObject();
        try {
            j.put("action", "udid_get_products");
            j.put("success", "true");
            j.put("content", "udid product list");
            j.put("data", nUdid);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.xxxxxxxxx.com/yyyy/test.php");
    }
}

这段代码存在于 oncreate 方法中

我需要做的是这个

 public class MainActivity extends Activity {

    ListView list1;
    LazyAdapter adapter;
    String nUdid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        myNewFunction();
    }


    public void myNewFunction() {
        final Context context = this;
        TextView viewtitle1 = (TextView) findViewById(R.id.textView1);

        JSONObject j = new JSONObject();
        try {
            j.put("action", "udid_get_products");
            j.put("success", "true");
            j.put("content", "udid product list");
            j.put("data", nUdid);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.xxxxxxxxx.com/yyyy/test.php");
    }
}

这会完全按原样工作还是我需要在函数名 myNewFunction 后面的 () 中做一些额外的事情。myNewFunction 语法中是否需要 oncreate 方法中的片段“Bundle savedInstanceState”。

4

1 回答 1

1

你所拥有的应该没问题。如果您需要在该方法中引用它,您只需要定义 myNewFunction 方法来获取 Bundle。根据您的示例,您目前在 myNewFunction 方法中不需要它。

但除非您需要在 MainActivity 之外调用 myNewFunction 方法,否则我个人会将其设为私有。

于 2012-11-28T06:34:33.787 回答