我的应用程序在单击按钮以启动活动时崩溃,但是可以在模拟器中使用

时间:2019-01-29 10:07:40

标签: java android onclick android-button

我有一个android应用程序,该应用程序从包含导航视图的主活动开始,还有一个名为fragment_home的片段,其中包含我的开始按钮。当我按下该按钮时,另一个活动开始。

在设备仿真器中可以正常工作,但是在我插入手机并直接构建到它时也可以使用。(Android 9的Pixel 3XL)

但是,如果我手动安装APK并在拔掉手机的情况下运行它,则主要活动会很好地打开并显示家庭片段内容,但是当我按下按钮时,应用程序立即崩溃。这是我的

的代码

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()函数是单击我主页片段中的按钮时运行的函数。

这是我的家庭片段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>

2 个答案:

答案 0 :(得分:1)

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

TMainF.HTTPReqResp1BeforePost

代替

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

您还可以发布所获得的错误吗?

答案 1 :(得分:1)

这可能是android 9错误! 在应用程序的清单文件中添加此行

<uses-library android:name="org.apache.http.legacy" android:required="false"/>