意向未启动新活动

时间:2019-02-12 13:43:01

标签: android android-intent android-activity

我正在尝试从意图中启动新活动,但是即使两种活动都扩展了AppCompatActivity,它也只能在一种情况下起作用。我不知道问题是什么。

启动器活动运行正常

<manifest>

<activity
        android:name=".activities.home_page.HomePageNavActivity"
        android:label="@string/title_activity_home_page_nav"
        android:theme="@style/AppTheme"
        android:windowSoftInputMode="adjustResize" />


<activity
        android:name=".activities.UserProfileActivity"
            android:parentActivityName=".activities.home_page.HomePageNavActivity"
        android:windowSoftInputMode="adjustResize" />

 <activity android:name=".activities.ProductDescription"
        android:parentActivityName=".activities.home_page.HomePageNavActivity"
        android:windowSoftInputMode="adjustResize"/>

MainActivity(HomePageNavActivity)

public class HomePageNavActivity extends AppCompatActivity              {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page_nav);


    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    navigation.setSelectedItemId(R.id.navigation_discover);



}

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment;
        switch (item.getItemId()) {

            case R.id.navigation_discover:

                   fragment = new DiscoverFragment();
                if(!Constant.currentBottomFragmentSelected.equals("discover") ) {
                    Constant.currentBottomFragmentSelected = "discover";
                    loadFragment(fragment);
                }

                return true;
            case R.id.navigation_inbox:
                Intent intent=new Intent(HomePageNavActivity.this,ProductDescription.class); //this intent is not working
                HomePageNavActivity.this.startActivity(intent);
                return true;



            case R.id.navigation_profile:
                Intent intent1 = new Intent(HomePageNavActivity.this, UserProfileActivity.class); //this intent is working
                startActivity(intent1);

                return true;
      }

        return false;
    }
};

活动1(产品说明)(不起作用)

public class ProductDescription extends AppCompatActivity {

FloatingActionButton fabLove,fabShare,fabCart;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_description);


    }

}

Activity2(UserProfileActivity)(正在运行)

    public class UserProfileActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_profile);
    utoolbar = (Toolbar) findViewById(R.id.toolbarProfile);
    setSupportActionBar(utoolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    imgBtn = findViewById(R.id.imgBtnSettings);
    imgBtnLogout = findViewById(R.id.imgBtnLogout);

   //remaining code consists of onClickListeners for different buttons
}
}

1 个答案:

答案 0 :(得分:0)

您必须删除HomePageNavActivity.this.startActivity(intent)

case R.id.navigation_inbox:
      Intent intent = new Intent(HomePageNavActivity.this,ProductDescription.class); 
      startActivity(intent);
      return true;