H.Walter
H.Walter

Reputation: 71

Why is my Intent starting an empty Activity instead of the right one?

Hopefully i can find some help here, because I'm looking for weeks already for a solution. I am using android studio for programming the app.

When I'm pressig the Button to start the new Activity it's opening an empty Activty instead of that one which was supposed to be opened.

The new Activity is coded in the Manifest, and there aren't any error notifications. Maybe its not getting the intent?

Here the MainActivity:

public class MainActivity extends AppCompatActivity {


    Toolbar toolbar;
    ViewPager viewPager;
    TabLayout tabLayout;

    FloatingActionButton fabtn1;
    FloatingActionButton fabtn2;
    FloatingActionButton fabtn3;
    FloatingActionButton fabtn4;

    int counter;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        viewPager = (ViewPager) findViewById(R.id.viewpager);

        ViewPagerAdapter1 viewPagerAdapter1 = new ViewPagerAdapter1(getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter1);

        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        fabtn1 = (FloatingActionButton) findViewById(R.id.fab1);
        fabtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                counter++;

                if ((counter % 2) == 1) {
                    fabtn2.animate().translationY(-200).setDuration(300).alpha(1);
                    fabtn3.animate().translationY(-400).setDuration(320).alpha(1);
                    fabtn4.animate().translationY(-600).setDuration(340).alpha(1);

                }
                if ((counter % 2) == 0) {
                    fabtn2.animate().translationY(0).setDuration(300).alpha(0);
                    fabtn3.animate().translationY(0).setDuration(320).alpha(0);
                    fabtn4.animate().translationY(0).setDuration(340).alpha(0);
                }
            }
        });

       //That's the Button, which should open the new Activity:

        fabtn2 = (FloatingActionButton) findViewById(R.id.fab2);
        fabtn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent Startmessageqp = new Intent(MainActivity.this, MessagesQP.class);
                startActivity(Startmessageqp);
            }
        });

        fabtn3 = (FloatingActionButton) findViewById(R.id.fab3);

        fabtn4 = (FloatingActionButton) findViewById(R.id.fab4);
    }
};

That's the new Activity which is supposed to be opened after pressing the button:

public class MessagesQP extends Activity {

    EditText editText1;
    EditText editText2;
    FloatingActionButton fabMQ1;
    FloatingActionButton fabQM2;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.message_quickpost);

        editText1 = (EditText) findViewById(R.id.editText);
        editText2 = (EditText) findViewById(R.id.editText2);
        fabMQ1 = (FloatingActionButton) findViewById(R.id.fabmq1);
        fabQM2 = (FloatingActionButton) findViewById(R.id.fabmq2);


    }
}

Please inform if you need some more informations to help and I'm thankful for every answer!

NEW: Here The messages_quickpost.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:text="Type Message"

    android:layout_marginTop="100dp"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="240dp"
    android:layout_height="60dp"
    android:inputType="textPersonName"
    android:text="Contact Name"
    android:id="@+id/editText2"

    android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/editText"
    android:layout_alignStart="@+id/editText"
    android:layout_marginTop="60dp" />


<android.support.design.widget.FloatingActionButton

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fabmq1"
    android:src="@drawable/send"

    android:layout_alignBottom="@+id/editText"
    android:layout_toRightOf="@+id/editText"
    android:layout_toEndOf="@+id/editText" />

<android.support.design.widget.FloatingActionButton

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fabmq2"
    android:src="@drawable/account_plus"
    android:layout_alignTop="@+id/editText2"
    android:layout_toLeftOf="@+id/editText2"
    android:layout_toStartOf="@+id/editText2" />


</RelativeLayout>

Thanks for helping!!

Upvotes: 2

Views: 1074

Answers (2)

H.Walter
H.Walter

Reputation: 71

Thanks for all your helps finally I found the solution. The on create part is wrong! I switched it now from:

 @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.message_quickpost);

to:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_quickpost);

That was the Problem.. Now its working! Still thanks to all of you!

Upvotes: 4

Jorgesys
Jorgesys

Reputation: 126455

it's opening an empty Activty instead of that one which was supposed to be opened.

Make a review for your message_quickpost layout, i can see that contain this elements editText1 , editText2 , fabMQ1 , fabMQ1 but you are not changing their properties like text, visibility etc.

setContentView(R.layout.message_quickpost);

editText1 = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
fabMQ1 = (FloatingActionButton) findViewById(R.id.fabmq1);
fabQM2 = (FloatingActionButton) findViewById(R.id.fabmq2);

Probably some of your elements has visibility property defined as INVISIBLE

Upvotes: 1

Related Questions