android - 为什么我的 Intent 启动一个空的 Activity 而不是正确的 Activity ?

标签 android android-intent android-studio

希望我能在这里找到一些帮助,因为我已经寻找解决方案几周了。我正在使用 android studio 来编写应用程序。

当我按下按钮启动新的 Activity 时,它会打开一个空的 Activity ,而不是应该打开的 Activity 。

新的 Activity 已编码在 Manifest 中,并且没有任何错误通知。也许它没有理解 Intent ?

这里是主要 Activity :

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

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

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>

感谢您的帮助!!

最佳答案

感谢大家的帮助,我终于找到了解决方案。创建部分是错误的!我现在将其切换自:

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

至:

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

这就是问题所在..现在它可以工作了!还是谢谢大家!

关于android - 为什么我的 Intent 启动一个空的 Activity 而不是正确的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35366073/

相关文章:

android eclipse 使用地址而不是纬度和经度来启动 map 的 Intent

java - 如何在 Android Studio 中通过 Intent 共享 GIF

android - 如何在 Android 上将 "Choose an action"更改为 "Complete action using"?

android - 如何在 Android Studio 中调整 AVD 模拟器窗口的大小?

安卓工作室 : Error: SplashActivity is not an Activity subclass or alias

java - 如何运行位于 android 模块内的单元测试?

android - LinearLayout 中具有相同权重的两个 View

java - 有什么方法可以将 256 字节 "MD5-like"字符串压缩为 160 字节或更少?

android - 创建 Galaxy S4 模拟器

java - Android 在运行时无法从 JAR 中找到方法