-2

我正在尝试通过 Intent 发送内容(我认为这是我错误的原因)并且我不断收到ActivityNotFoundException. 这是向Tab1.class(称为温度的字符串)发送数据的类

我在堆栈上发现了类似的问题,有人写道,问题是由在活动完成其生命周期方法之前实例化意图引起的,但我不确定这是否是一个问题,我应该怎么做才能解决它

public class ChooseLayout extends AppCompatActivity implements View.OnClickListener {

    EditText editText;
    Button button;
    Weather weather;
    Parser parser = new Parser();
    SharedPreferences sharedPreferences;
    String temperature, preassure,WindSpeed,WindDirection,Humidity,ViewDescription;
    boolean isChecked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_layout);
        editText = (EditText) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isOnline()) {
                    Toast.makeText(getApplicationContext(), "Cannot connect to network, data will be restore from file with last downloaded data...", Toast.LENGTH_LONG).show();
                } else {
                    Thread t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                weather = parser.getWeatherForLocation(editText.getText() + "");
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        editText.setText(weather.getCity() + ",  " + weather.getCountry());
                                         temperature = weather.getTemperature();
                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    t.start();
                }
                Intent intent = new Intent(getApplicationContext(),Tab1.class);
                intent.putExtra("temperature", temperature);
                startActivity(intent);


            }
        });
        sharedPreferences = getSharedPreferences("file_name", MODE_PRIVATE);
    }

我已经在 AndroidManifest.xml 中声明了这个活动:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chirag.slidingtabsusingviewpager">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ChooseLayout"></activity>

    </application>

</manifest>
4

1 回答 1

0

您可以在活动中添加或替换片段。

然后在您的活动中执行此操作以进行add分段:

FragmentManager manager = getSupportFragmentManager();
Tab1 tab1 = new Tab1();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(android.R.id.content, tab1);
transaction.commit();

replace分段执行此操作:

FragmentManager manager = getSupportFragmentManager();
Tab1 tab1 = new Tab1();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(android.R.id.content, tab1);
transaction.commit();

要从fragment您那里传递数据,可以使用Bundleas,

Bundle bundle = new Bundle();
bundle.putString("your_tag", your_string);
FragmentManager manager = getSupportFragmentManager();
Tab1 tab1 = new Tab1();
tab1.setArguments(bundle);
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(android.R.id.content, tab1);
transaction.commit();

要了解更多信息,fragment-transaction请查看此https://developer.android.com/reference/android/app/FragmentTransaction.html

于 2018-01-02T16:19:31.033 回答