gpt4 book ai didi

java - 我的应用程序在单击按钮开始 Activity 时崩溃,但在模拟器中工作

转载 作者:搜寻专家 更新时间:2023-11-01 09:21:53 25 4
gpt4 key购买 nike

我有一个 android 应用程序,它从包含导航 View 的主 Activity 开始,还有一个名为 fragment_home 的 fragment ,它有我的开始按钮。当我按下那个按钮时,另一个 Activity 开始了。这在设备模拟器中工作正常,但当我插入手机并直接构建它时它也可以工作(Pixel 3XL with Android 9)

但是,如果我手动安装 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com