Android异步任务,活动之间传递字符串

3
我正在尝试从一个活动将由下拉框设置的字符串(locationSet)传递到另一个活动,它们两个在各自的类中都实现了一个Asynchtask。
我无法从MainActivity传递值到我的WeatherActivity。使用日志,我可以看到MainActivity中初始值设置得很好,但是每次我尝试将其传递到WeatherActivity时都为null。如果我手动输入相关字符串到WeatherActivity中,则输出按预期工作。
我已经查看了其他问题,但我发现很难应用我所看到的任何内容。 MainActivity.java
public class MainActivity extends Activity {

Button searchLocation;
Spinner locationSpinner;
String locationSet = null;
String strLocation = null;
Locations arrLocation;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    searchLocation = (Button) findViewById(R.id.searchLocationButton);

    locationSpinner = (Spinner) findViewById(R.id.locationsListView);

    ArrayAdapter<CharSequence> adaptor = ArrayAdapter.createFromResource(this, R.array.location_array, android.R.layout.simple_list_item_1);
    locationSpinner.setAdapter(adaptor);

    searchLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new MyAsync().execute();

        }
    });
}

public void onBackPressed() {
    super.onBackPressed();
    this.finish();
}

class MyAsync extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        strLocation = locationSpinner.getSelectedItem().toString();

        if (strLocation.equals("Aberdeen")) {
            locationSet = arrLocation.ABERDEEN;
        } else if (strLocation.equals("Dundee")) {
            locationSet = arrLocation.DUNDEE;
        } else if (strLocation.equals("Edinburgh")) {
            locationSet = arrLocation.EDINBURGH;
        } else if (strLocation.equals("Fort William")) {
            locationSet = arrLocation.FORT_WILLIAM;
        } else if (strLocation.equals("Glasgow")) {
            locationSet = arrLocation.GLASGOW;
        } else if (strLocation.equals("Manchester")) {
            locationSet = arrLocation.MANCHESTER;
        } else if (strLocation.equals("North Berwick")) {
            locationSet = arrLocation.NORTH_BERWICK;
        } else if (strLocation.equals("Portree")) {
            locationSet = arrLocation.PORTREE;
        } else if (strLocation.equals("Ullapool")) {
            locationSet = arrLocation.ULLAPOOL;
        }

        Intent intent = new Intent(MainActivity.this, WeatherActivity.class);
        startActivity(intent);
    }
}

}

WeatherActivity.java

    public class WeatherActivity extends MainActivity {

    TextView firstDayTextView;

    String selectedLocation = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather_activity);

        locationTextView = (TextView) findViewById(R.id.locationTextView);



        new LoginAsync().execute();
    }

    class LoginAsync extends AsyncTask<Void, Void, Void> {
        MySaxHandler myHandler;

        StringBuilder builder = new StringBuilder();

        @Override
        protected Void doInBackground(Void... params) {
            myHandler = new MySaxHandler();
            myHandler.addLocation(selectedLocation.toString());
            myHandler.get();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            ArrayList<ItemData> items = myHandler.items;

            if (null != items && items.size() != 0) {
                for (int index = 0; index < items.size(); index++) {
                    ItemData obJPost = items.get(index);
                        builder.append("\n" + obJPost.getItemTitle());
                }
            }
            firstDayTextView.setText(builder.toString());

        }
    }
}

1
你忘记了从你的 MainActivity 到你的 WeatherActivity 进行 intent.putExtra("STRING_I_NEED", locationSet);。确保在 WeatherActivity 中获取 Intent,并以相同 ID 发送字符串,即此处为 STRING_I_NEED。希望这可以帮到你。 - user2652394
1
不需要在MainActivity中使用asynctask,只需将值放入意图中并在WeatherActivity中获取即可。 - Ketan Ahir
感谢您的回复,我在下面说了解决方案已经改善了一件事情,但并没有解决所有问题。 - TheBluePotter
1个回答

2

在启动天气活动时,您可以使用Intent.putExtra("TAG", locationSet)传递字符串值“locationSet”。在onCreate中,您可以使用Intent.getExtra获取该值。

示例代码:

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String keyIdentifer  = null;
i.putExtra("STRING_I_NEED", strName);

Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
    extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

谢谢您的回复,现在我的WeatherActivity日志显示了一个值,但是当我切换活动时它崩溃了,我很困惑。我不知道我的代码中可能有什么错误? - TheBluePotter
请问您能否在这里添加崩溃日志?或者您能告诉我它是在哪一行崩溃的吗? - Sushil
我已经添加了日志的副本,希望这足够提供信息。我可以立即看到URL应该在null处有一个表示位置的字符串。 - TheBluePotter
在WeatherActivity中,您将字符串locationSet存储到一个"test"字符串中。在doInBackground()方法中,您正在将"locationSet"添加到处理程序中。您应该添加"test"。无论如何,现在似乎是其他错误,与您最初的问题无关 :) - Sushil
我已经解决了,是的,我注意到了,我没有在这里添加更改。谢谢你的帮助。selectedLocation.toString(),我从来没有使用过.toString()我感觉很傻,但很高兴它正在工作。 - TheBluePotter
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接