Java 我的应用程序在单击按钮开始活动时崩溃,但在emulator中工作
我有一个android应用程序,它从包含导航视图的主活动开始,还有一个名为fragment_home的片段,它有我的开始按钮。当我按下那个按钮时,另一个活动开始了。这在设备仿真器中运行良好,但当我插入手机并直接构建到它时也可以运行(Android 9的像素3XL) 但是,如果我手动安装APK并在手机未拔下的情况下运行它,主活动将正常打开并显示主片段内容,但当我按下按钮时,应用程序立即崩溃。这是我的密码 MainActivity.javaJava 我的应用程序在单击按钮开始活动时崩溃,但在emulator中工作,java,android,onclick,android-button,Java,Android,Onclick,Android Button,我有一个android应用程序,它从包含导航视图的主活动开始,还有一个名为fragment_home的片段,它有我的开始按钮。当我按下那个按钮时,另一个活动开始了。这在设备仿真器中运行良好,但当我插入手机并直接构建到它时也可以运行(Android 9的像素3XL) 但是,如果我手动安装APK并在手机未拔下的情况下运行它,主活动将正常打开并显示主片段内容,但当我按下按钮时,应用程序立即崩溃。这是我的密码 MainActivity.java import android.content.Intent
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()
函数是在单击my home片段中的按钮时运行的函数
这是我的主页片段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片段? 如果您需要使用
getSupportFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
而不是
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
您还可以发布获得的错误吗?这可能是android 9错误! 在应用程序内的清单文件中添加此行
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
发生崩溃时,会出现stacktrace。检查日志并添加带有问题的错误日志。你能告诉我错误吗?当我在物理设备上运行应用程序时,当它被拔出时,我如何检查错误?插入设备->在物理设备上运行应用程序->检查日志错误并更新问题如果我插入设备并在其上运行应用程序,它会工作。只有当我手动安装并在手机拔下时运行它时,它才会崩溃。我如何知道我使用的是哪些片段?起初我使用的是v4,但我将其全部更改为常规片段。此外,我无法获得该错误,因为只有当设备拔出时应用程序才会崩溃。app.Fragment现在已被弃用(从API级别28开始),每个人都应该使用支持库实现。因此,我需要做哪些更改才能做到这一点?它是gradle中的某个东西吗?您的片段只需要扩展v4片段(android.support.v4.app.fragment)例如:MyFragment扩展片段{…}我是否导入android.app.fragment?还是另一个图书馆?