应用程序在发布时崩溃

时间:2017-11-10 10:11:35

标签: java android

以下是一个简单的SQLite示例的MainActivity。该应用程序在发布时崩溃。以下是错误(粗体):

11-10 10:40:41.243 1773-1773/? I/art: Late-enabling -Xcheck:jni
11-10 10:40:41.286 1773-1773/com.mytestfyp.ahmed.database V/Theme: ColorMode:com.mytestfyp.ahmed.database is false
11-10 10:40:41.328 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.473 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.513 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.553 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.593 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.631 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.669 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.706 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.747 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.788 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.836 1773-1773/com.mytestfyp.ahmed.database W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.mytestfyp.ahmed.database-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.mytestfyp.ahmed.database-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
11-10 10:40:41.837 1773-1773/com.mytestfyp.ahmed.database W/System: ClassLoader referenced unknown path: /data/app/com.mytestfyp.ahmed.database-1/lib/arm64
11-10 10:40:41.840 1773-1773/com.mytestfyp.ahmed.database I/InstantRun: starting instant run server: is main process
11-10 10:40:41.843 1773-1773/com.mytestfyp.ahmed.database I/Instrumentation: android.app.Application@103d181 onCreate begin
11-10 10:40:41.843 1773-1773/com.mytestfyp.ahmed.database I/Instrumentation: android.app.Application@103d181 onCreate in 0ms
11-10 10:40:41.900 1773-1773/com.mytestfyp.ahmed.database W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-10 10:40:42.003 1773-1773/com.mytestfyp.ahmed.database D/AndroidRuntime: Shutting down VM
11-10 10:40:42.003 1773-1773/com.mytestfyp.ahmed.database E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.mytestfyp.ahmed.database, PID: 1773
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mytestfyp.ahmed.database/com.mytestfyp.ahmed.database.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnLongClickListener(android.view.View$OnLongClickListener)' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
                                                                                at android.app.ActivityThread.access$900(ActivityThread.java:155)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:152)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5497)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnLongClickListener(android.view.View$OnLongClickListener)' on a null object reference
                                                                                **at com.mytestfyp.ahmed.database.MainActivity.displayAllRecords(MainActivity.java:143)
                                                                                at com.mytestfyp.ahmed.database.MainActivity.onCreate(MainActivity.java:45)**
                                                                                at android.app.Activity.performCreate(Activity.java:6289)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 
                                                                                at android.app.ActivityThread.access$900(ActivityThread.java:155) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:152) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5497) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
11-10 10:40:42.005 1773-1773/com.mytestfyp.ahmed.database D/AppTracker: App Event: crash
11-10 10:40:44.108 1773-1773/com.mytestfyp.ahmed.database I/Process: Sending signal. PID: 1773 SIG: 9

以下是MainActivity的代码:

package com.mytestfyp.ahmed.database;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity
{
    Button btnAddNewRecord;
    SQLiteHelper sQLiteHelper;

    android.widget.LinearLayout parentLayout;
    LinearLayout layoutDisplayPeople;

    TextView tvNoRecordsFound;
    private String rowID = null;

    private ArrayList<HashMap<String, String>> tableData = new ArrayList<HashMap<String, String>>();

    private class Holder
    {
        TextView tvFullName;
        String name;
        String age;
    }

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

        getAllWidgets();
        sQLiteHelper = new SQLiteHelper(MainActivity.this);
        bindWidgetsWithEvent();
        displayAllRecords();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK)
        {
            //firstname & lastname
            String name = data.getStringExtra(Constants.NAME);
            String age = data.getStringExtra(Constants.AGE);

            ContactModel contact = new ContactModel();
            contact.setName(name);
            contact.setAge(age);

            if (requestCode == Constants.ADD_RECORD)
            {
                sQLiteHelper.insertRecord(contact);
            }

            else if (requestCode == Constants.UPDATE_RECORD)
            {
                contact.setID(rowID);
                sQLiteHelper.updateRecord(contact);
            }

            displayAllRecords();

        }
    }

    private void getAllWidgets()
    {
        btnAddNewRecord = (Button) findViewById(R.id.btnAddNewRecord);
        parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
        tvNoRecordsFound = (TextView) findViewById(R.id.tvNoRecordsFound);
    }

    private void bindWidgetsWithEvent()
    {
        btnAddNewRecord.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                onAddRecord();
            }
        });
    }

    private void onAddRecord()
    {
        Intent intent = new Intent(MainActivity.this, TableManipulationActivity.class);
        intent.putExtra(Constants.DML_TYPE, Constants.ADD_RECORD);
        startActivityForResult(intent, Constants.ADD_RECORD);
    }

    private void onUpdateRecord(String name, String age)
    {
        Intent intent = new Intent(MainActivity.this, TableManipulationActivity.class);
        intent.putExtra(Constants.NAME, name);
        intent.putExtra(Constants.AGE, age);
        intent.putExtra(Constants.DML_TYPE, Constants.UPDATE);
        startActivityForResult(intent, Constants.UPDATE_RECORD);
    }

    private void displayAllRecords()
    {
        LinearLayout inflateParentView;
        parentLayout.removeAllViews();

        ArrayList<ContactModel> contacts = sQLiteHelper.getAllRecords();

        if (contacts.size() > 0)
        {
            tvNoRecordsFound.setVisibility(View.GONE);
            ContactModel contactModel;

            for (int i = 0; i < contacts.size(); i++)
            {
                contactModel = contacts.get(i);

                final Holder holder = new Holder();
                final View view = LayoutInflater.from(this).inflate(R.layout.inflate_record, null);
                inflateParentView = (LinearLayout) findViewById(R.id.inflateParentView);
                holder.tvFullName = (TextView) view.findViewById(R.id.tvFullName);

                view.setTag(contactModel.getID());
                holder.name = contactModel.getName();
                holder.age = contactModel.getAge();

                String personData = holder.name + " " +holder.age;
                holder.tvFullName.setText(personData);

                final CharSequence[] items = {Constants.UPDATE, Constants.DELETE};

                inflateParentView.setOnLongClickListener(new View.OnLongClickListener()
                {
                    @Override
                    public boolean onLongClick(View v)
                    {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setItems(items, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                if (which == 0)
                                {
                                    rowID = view.getTag().toString();
                                    onUpdateRecord(holder.name, holder.age);
                                }

                                else
                                {
                                    AlertDialog.Builder deleteDialogOK = new AlertDialog.Builder(MainActivity.this);
                                    deleteDialogOK.setTitle("Delete Person ?");

                                    deleteDialogOK.setPositiveButton("OK", new DialogInterface.OnClickListener()
                                    {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which)
                                        {
                                            ContactModel contact = new ContactModel();
                                            contact.setID(view.getTag().toString());
                                            sQLiteHelper.deleteRecord(contact);
                                            displayAllRecords();
                                        }
                                    });
                                    deleteDialogOK.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                                    {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which)
                                        {

                                        }
                                    });
                                    deleteDialogOK.show();
                                }
                            }
                        });
                        AlertDialog alertDialog = builder.create();
                        alertDialog.show();
                        return true;
                    }
                });
                parentLayout.addView(view);
            }
        }

        else
        {
            tvNoRecordsFound.setVisibility(View.VISIBLE);
        }
    }
}

以下是相关的XML:

activitymain.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ScrollView
        android:id="@+id/scrollViewDisplay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/layoutDisplayPeople"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/people"
                    android:textColor="@color/color_black"
                    android:textSize="25sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/tvNoRecordsFound"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="15dp"
                    android:layout_marginTop="15dp"
                    android:gravity="center"
                    android:text="@string/nrf"
                    android:textColor="@color/color_black"
                    android:textSize="15sp" />

                <LinearLayout
                    android:id="@+id/parentLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"/>

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/btnAddNewRecord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_blue"
        android:text="@string/add"
        android:textColor="@color/color_white"
        android:textStyle="bold" />
</LinearLayout>

inflate_record.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <LinearLayout
        android:id="@+id/inflateParentView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <TextView
            android:id="@+id/tvFullName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginStart="15dp"
            android:layout_weight="1"
            android:text="@string/name"
            android:textSize="15sp"
            android:gravity="start"
            android:textColor="@color/color_black"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/color_grey"/>

</LinearLayout>

请帮助我。提前谢谢!

3 个答案:

答案 0 :(得分:1)

您的应用崩溃是因为在尝试在 NULL inflateParentView上调用setOnLongClickListener()时出现NullPointerException。确保视图不为空。

答案 1 :(得分:1)

inflateParentView为null,因为它未实例化。在activity中调用findViewById()将在特定活动布局中找到id。您需要从自定义布局中找到该视图。因此更改此行。

inflateParentView = (LinearLayout) findViewById(R.id.inflateParentView);

inflateParentView = (LinearLayout) view.findViewById(R.id.inflateParentView);

答案 2 :(得分:1)

试试这个:

inflateParentView = (LinearLayout)view.findViewById(R.id.inflateParentView);

NullPointerException您获得了inflateParentView。这是因为您试图从活动布局而不是从膨胀的视图中查找视图。希望它有所帮助:)