java - 我的应用程序在单击按钮启动 Activity 时崩溃,但可以在模拟器中运行

标签 java android onclick android-button

我有一个Android应用程序,它从包含导航 View 的主要 Activity 开始,以及一个名为fragment_home的 fragment ,其中有我的开始按钮。当我按下该按钮时,另一个 Activity 就会开始。这在设备模拟器中运行良好,但当我插入手机并直接构建时它也可以运行(搭载 Android 9 的 Pixel 3XL)

但是,如果我手动安装 APK 并在拔掉手机电源的情况下运行它,主 Activity 会正常打开并显示主 fragment 内容,但当我按下按钮时,应用程序会立即崩溃。这是我的代码

MainActivity.java

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        /*
        *   Avoid reloading fragment when device is rotated
        */

        if(savedInstanceState == null)
        {
            getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
            navigationView.setCheckedItem(R.id.fragment_home);
        }

    }

    /*  Add functionality to the navigation drawer items    */
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        switch(menuItem.getItemId()) {
            case R.id.home:
                getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
                break;
            case R.id.history:
                getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HistoryFragment()).commit();
                break;
            case R.id.settings:
                getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new SettingsFragment()).commit();
                break;
            case R.id.help:
                getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HelpFragment()).commit();
                break;
        }

        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mToggle.onOptionsItemSelected(item))
            return true;
        return super.onOptionsItemSelected(item);
    }


    public void buttonClick(View v) {
        switch(v.getId()) {
            case R.id.startButton:

                /*  Start the survey activity   */
                Intent myIntent = new Intent(MainActivity.this, SurveyClass.class);
                startActivity(myIntent);

                break;
        }
    }
}

buttonClick() 函数是在单击我的主页 fragment 中的按钮时运行的函数。

这是我的主页 fragment XML 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_home"
    android:background="@color/colorPrimary">

    <Button
        android:id="@+id/startButton"
        android:layout_width="256dp"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="120dp"
        android:background="@drawable/button_rounded_corners_white"
        android:fontFamily="@font/segoeuib"
        android:gravity="center"
        android:includeFontPadding="false"
        android:onClick="buttonClick"
        android:text="FIND PRODUCTS"
        android:textColor="@color/colorAccent"
        android:textSize="26sp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="326dp"
        android:layout_height="188dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="29dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="18dp"
        app:srcCompat="@drawable/logo_white" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView2"
        android:layout_centerHorizontal="true"
        android:textColor="@color/white"
        android:textSize="24dp"
        android:textStyle="bold"
        android:text="@string/subtitle" />
</RelativeLayout>

最佳答案

您是否使用 Android 支持 v4 Fragment ? 如果是这种情况,您需要使用

 getSupportFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();

而不是

 getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();

你能把得到的错误也贴出来吗?

关于java - 我的应用程序在单击按钮启动 Activity 时崩溃,但可以在模拟器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418510/

相关文章:

java - 从任何数据集合中删除重复项的通用方法

java - 在运行时为 Android 执行嵌入在 Java 中的 Groovy 脚本

android - 我如何在android中为挂起功能和状态流编写单元测试

javascript - Onblur 事件在另一个 div 的 onclick 之前触发

php - Javascript onclick改变第二个输入框的值与第一个输入框的值相同

java - 您可以在 map 值上指定泛型类型吗?

java - Shiro 在 session 过期/失效时抛出异常

java - 在 Spring MVC/Hibernate 中简单地更新多个值

java - 无法在 Java 中初始化 Singleton 类中的静态成员 - Android

jquery - 单击提交表单之前确认对话框