-2

I'm trying to send content through Intent (I think this is the reason of my error) and I constantly get ActivityNotFoundException. Here's the class which is sending data to Tab1.class (String called temperature)

I found similar question on stack where someone wrote that problem is caused by instantiating the intent before the activity has gone through its life cycle methods but I'm not sure if it is a problem and what should I do to fix it

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

I have declared this activity in 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>
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
PiiiTeeeR
  • 45
  • 3

1 Answers1

0

You can either add or replace fragment in your activity.

Then do this in your activity to add fragment:

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

Or to replace fragment do this:

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

To pass data from fragment you can use Bundle as,

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

To know about more fragment-transaction check this https://developer.android.com/reference/android/app/FragmentTransaction.html

Aditya
  • 3,525
  • 1
  • 31
  • 38
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/162369/discussion-on-answer-by-heisen-berg-activitynotfoundexception-unable-to-find-ex). – Andy Jan 02 '18 at 18:45