无法更改全局变量

时间:2018-08-17 08:49:19

标签: java android function variables global

item1是同一类中的全局变量, 但是即使使用了此功能,我也无法更改它。

如果我使用item1,它说item1为空

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //..dequeue your cell
    cell.commentDelegate = self
}

func didSelectCell(_ cell: CommentCell) {
    //do what's needed here
}

这是我的全班。 我正在获取java.lang.NullPointerException:println需要一条消息 我猜是从Log.i(“ msg”,item1)

/ ********全班********* /

打包com.example.kimsy.myapplication;

public void getItemFromServer(String id){
    retrofit=new Retrofit.Builder().baseUrl(ApiService.API_URL).build();
    apiService=retrofit.create(ApiService.class);

    //GET
    Call<ResponseBody> comment=apiService.getByID(id);
    comment.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                item1=response.body().string();
                try{Thread.sleep(2000);}catch(InterruptedException e){Log.i("sleep", "failed2");}

            }catch (IOException e){
                Log.i("Test2", "failed");
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.i("Test", "get item from server failed");
        }
    });


}

1 个答案:

答案 0 :(得分:0)

您不应使用这种方式

 getItemFromServer("5");

 Log.i("msg", item1);

 String item2="item2";
 String item3="item3";

 String[] items={item1, item2, item3};
 ListAdapter adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
 ListView listView=(ListView)findViewById(R.id.listView);

 listView.setAdapter(adapter);

因为getItemFromServer方法正在调用api,而该方法正在另一个线程上工作,这意味着您调用

String[] items={item1, item2, item3};

item1也许仍然null

所以,让我们以正确的方式做到这一点:

public class MainActivity extends AppCompatActivity {
Retrofit retrofit;
ApiService apiService;

String item1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Log.i("result", getItemFromServer("5"));

    Log.i("msg", item1);

    String item2="item2";
    String item3="item3";

    ListView listView=(ListView)findViewById(R.id.listView);

    listView.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                    String item=String.valueOf(parent.getItemAtPosition(i));
                    Toast.makeText(MainActivity.this, item, Toast.LENGTH_LONG).show();
                }
            }
    );
    getItemFromServer("5");
}

public void getItemFromServer(String id){
    retrofit=new Retrofit.Builder().baseUrl(ApiService.API_URL).build();
    apiService=retrofit.create(ApiService.class);


    //GET
    Call<ResponseBody> comment=apiService.getByID(id);
    comment.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                item1=response.body().string();
            String[] items={item1, item2, item3};
            ListAdapter adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
            listView.setAdapter(adapter);
            }catch (IOException e){
                Log.i("Test2", "failed");
            }
        }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.i("Test", "get item from server failed");
    }
});

} }