为什么我的Intent开始一个空的Activity而不是正确的Activity?

时间:2016-02-12 15:18:48

标签: android android-intent android-studio

希望我能在这里找到一些帮助,因为我已经找了几个星期才找到解决方案。我正在使用android studio对应用程序进行编程。

当我按下按钮开始新的活动时,它打开一个空的Activty而不是那个本应打开的Activty。

新的活动在Manifest中编码,并且没有任何错误通知。也许它没有得到意图?

这里是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);
    }
};

这是按下按钮后应该打开的新活动:

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


    }
}

如果您需要更多信息来帮助我们,请通知我,我感谢您的每一个答案!

新:这里是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>

感谢您的帮助!!

2 个答案:

答案 0 :(得分:2)

感谢您的帮助,最后我找到了解决方案。 on create部分是错误的!我现在切换它:

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

为:

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

那是问题..现在它的工作!还是谢谢你们所有人!

答案 1 :(得分:1)

  它打开了一个空的Activty,而不是那个假想的那个   打开。

对您的message_quickpost布局进行审核,我可以看到其中包含此元素editText1 , editText2 , fabMQ1 , fabMQ1,但您不会更改其属性,如textvisibility等。

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

可能有些元素的可见性属性定义为INVISIBLE