简体   繁体   中英

App crashing when running on devices, when going to main activity

So the app works on emulator perfectly, however when i tried to run it on devices, it crash on main activity. please i need help, will really appreciate it.

heres the main activity java

package com.example.dashboard;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.DisplayCutout;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView day,month,year;

    public CardView draftCard, cardsCard, todoCard, moneyCard, linkedinCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        //definingdates

        day = findViewById(R.id.day);
        month = findViewById(R.id.month);
        year = findViewById(R.id.year);

        Date currentTime = Calendar.getInstance().getTime();
        String formattedDate = DateFormat.getDateInstance(DateFormat.FULL).format(currentTime);
        String[] splitDate = formattedDate.split(",");


        Log.d("myLog", currentTime.toString());
        Log.d("myLog", formattedDate);

        //setdate

        month.setText(splitDate[1]);
        day.setText(splitDate[0]);
        year.setText(splitDate[2]);

        Log.d("myLog", splitDate[0].trim());
        Log.d("myLog", splitDate[1].trim());
        Log.d("myLog", splitDate[2].trim());

        //definingcards
        draftCard = findViewById(R.id.draftCard);
        cardsCard = findViewById(R.id.cardsCard);
        moneyCard = findViewById(R.id.moneyCard);
        todoCard = findViewById(R.id.todoCard);
        linkedinCard = findViewById(R.id.linkedinCard);

        //addingclicklistener
        draftCard.setOnClickListener(this);
        cardsCard.setOnClickListener(this);
        moneyCard.setOnClickListener(this);
        todoCard.setOnClickListener(this);
        linkedinCard.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        Intent i ;

        switch (v.getId()) {
            case R.id.draftCard: i = new Intent(this, draftcard.class);
                startActivity(i);
                break;
            case R.id.cardsCard: i = new Intent(this, cardscard.class);
                startActivity(i);
                break;
            case R.id.moneyCard: i = new Intent(this, moneycard.class);
                startActivity(i);
                break;
            case R.id.todoCard: i = new Intent(this, todocard.class);
                startActivity(i);
                break;
            case R.id.linkedinCard: i = new Intent(this, linkedincard.class);
                startActivity(i);
                break;
            default:break;
        }

    }
}

and the stack traces

2021-01-28 01:29:52.465 28496-28496/? I/ample.dashboar: Late-enabling -Xcheck:jni
2021-01-28 01:29:52.517 28496-28496/? E/ample.dashboar: Unknown bits set in runtime_flags: 0x8000
2021-01-28 01:29:52.528 28496-28496/? I/ample.dashboar: Reinit property: dalvik.vm.checkjni= false
2021-01-28 01:29:52.567 28496-28496/? E/libc: Access denied finding property "runtime.mmitest.isrunning"
2021-01-28 01:29:52.579 28496-28496/? D/ActivityThread: Attach thread to application
2021-01-28 01:29:52.761 28496-28496/com.example.dashboard I/ample.dashboar: QarthPatchMonintor::Init
2021-01-28 01:29:52.761 28496-28496/com.example.dashboard I/ample.dashboar: QarthPatchMonintor::StartWatch
2021-01-28 01:29:52.762 28496-28496/com.example.dashboard I/ample.dashboar: QarthPatchMonintor::WatchPackage: /data/hotpatch/fwkhotpatch/
2021-01-28 01:29:52.762 28496-28496/com.example.dashboard I/ample.dashboar: QarthPatchMonintor::CheckAndWatchPatch: /data/hotpatch/fwkhotpatch/com.example.dashboard
2021-01-28 01:29:52.762 28496-28496/com.example.dashboard I/ample.dashboar: QarthPatchMonintor::CheckAndWatchPatch: /data/hotpatch/fwkhotpatch/all
2021-01-28 01:29:52.762 28496-28496/com.example.dashboard I/ample.dashboar: QarthPatchMonintor::Run
2021-01-28 01:29:52.764 28496-28564/com.example.dashboard I/ample.dashboar: QarthPatchMonintor::Reading
2021-01-28 01:29:52.764 28496-28564/com.example.dashboard I/ample.dashboar: QarthPatchMonintor::CheckNotifyEvent
2021-01-28 01:29:52.764 28496-28564/com.example.dashboard I/ample.dashboar: QarthPatchMonintor::CheckNotifyEvent before read
2021-01-28 01:29:52.780 28496-28539/com.example.dashboard I/HwApiCacheMangerEx: apicache path=/storage/emulated/0 state=mounted key=com.example.dashboard#10312#256
2021-01-28 01:29:52.780 28496-28539/com.example.dashboard I/HwApiCacheMangerEx: apicache path=/storage/1538-291C state=mounted key=com.example.dashboard#10312#256
2021-01-28 01:29:52.786 28496-28539/com.example.dashboard I/HwApiCacheMangerEx: apicache path=/storage/emulated/0 state=mounted key=com.example.dashboard#10312#0
2021-01-28 01:29:52.786 28496-28539/com.example.dashboard I/HwApiCacheMangerEx: apicache path=/storage/1538-291C state=mounted key=com.example.dashboard#10312#0
2021-01-28 01:29:52.800 28496-28539/com.example.dashboard I/AwareBitmapCacher: init processName:com.example.dashboard pid=28496 uid=10312
2021-01-28 01:29:52.857 28496-28577/com.example.dashboard E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@a6ca3cf
2021-01-28 01:29:52.959 28496-28496/com.example.dashboard V/ActivityThread: callActivityOnCreate
2021-01-28 01:29:52.989 28496-28496/com.example.dashboard V/HwWidgetFactory: : successes to get AllImpl object and return....
2021-01-28 01:29:53.029 28496-28496/com.example.dashboard W/ample.dashboar: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-01-28 01:29:53.031 28496-28496/com.example.dashboard W/ample.dashboar: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-01-28 01:29:53.120 28496-28496/com.example.dashboard D/ActivityThread: add activity client record, r= ActivityRecord{6e77753 token=android.os.BinderProxy@3a3b4ad {com.example.dashboard/com.example.dashboard.Splash}} token= android.os.BinderProxy@3a3b4ad
2021-01-28 01:29:53.171 28496-28618/com.example.dashboard D/HiTouch_PressGestureDetector: onAttached, package=com.example.dashboard, windowType=1, mHiTouchRestricted=false
2021-01-28 01:29:53.188 28496-28570/com.example.dashboard I/iGraphics: [0020080c] pn: com.example.dashboard, p: 28496
2021-01-28 01:29:53.188 28496-28570/com.example.dashboard I/iGraphics: [0030080c] no spt app: com.example.dashboard
2021-01-28 01:29:53.216 28496-28570/com.example.dashboard D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2021-01-28 01:29:53.225 28496-28570/com.example.dashboard W/Gralloc3: mapper 3.x is not supported
2021-01-28 01:29:53.318 28496-28496/com.example.dashboard I/HwViewRootImpl: removeInvalidNode jank list is null
2021-01-28 01:29:57.800 28496-28496/com.example.dashboard D/AwareBitmapCacher: handleInit switch not opened pid=28496
2021-01-28 01:29:57.918 28496-28496/com.example.dashboard D/HwFrameworkSecurityPartsFactory: HwFrameworkSecurityPartsFactory in.
2021-01-28 01:29:57.919 28496-28496/com.example.dashboard I/HwFrameworkSecurityPartsFactory: add HwFrameworkSecurityPartsFactory to memory.
2021-01-28 01:29:57.972 28496-28496/com.example.dashboard W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@94573fa
2021-01-28 01:29:57.987 28496-28496/com.example.dashboard V/ActivityThread: callActivityOnCreate
2021-01-28 01:29:58.012 28496-28496/com.example.dashboard I/OverScrollerOptimization: start init SmartSlideOverScroller and get the overscroller config
2021-01-28 01:29:58.012 28496-28496/com.example.dashboard I/OverScrollerOptimization: get the overscroller config
2021-01-28 01:29:58.111 28496-28496/com.example.dashboard D/myLog: Thu Jan 28 01:29:58 GMT+08:00 2021
2021-01-28 01:29:58.111 28496-28496/com.example.dashboard D/myLog: Thursday, 28 January 2021
2021-01-28 01:29:58.112 28496-28496/com.example.dashboard I/BlockMonitor: dispatchingThrewException In MainThread
2021-01-28 01:29:58.112 28496-28496/com.example.dashboard D/AndroidRuntime: Shutting down VM
2021-01-28 01:29:58.112 28496-28496/com.example.dashboard I/QarthLog: [PatchStore] createDisableExceptionQarthFile
2021-01-28 01:29:58.112 28496-28496/com.example.dashboard I/QarthLog: [PatchStore] create disable file for com.example.dashboard uid is 10312
2021-01-28 01:29:58.114 28496-28496/com.example.dashboard E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.dashboard, PID: 28496
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dashboard/com.example.dashboard.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3961)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
        at com.example.dashboard.MainActivity.onCreate(MainActivity.java:47)
        at android.app.Activity.performCreate(Activity.java:8086)
        at android.app.Activity.performCreate(Activity.java:8074)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1313)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3755)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3961) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:213) 
        at android.app.ActivityThread.main(ActivityThread.java:8178) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 
2021-01-28 01:29:58.133 28496-28496/com.example.dashboard I/Process: Sending signal. PID: 28496 SIG: 9

i tried already removing the year.setText(splitDate[2]); and log for 2, everything works well. but i really need to know what really the problems as the other month and day works pretty well.

thank you guys

Format date in the format you definitely control. For example:

Date currentTime = Calendar.getInstance().getTime();
String formatted = (new SimpleDateFormat("dd MMMM yyyy")).format(currentDate);
String[] splitDate = formatted.split(" ");

month.setText(splitDate[1]);
day.setText(splitDate[0]);
year.setText(splitDate[2]);

Print out formattedDate and make sure that delimiter (comma) is the correct choice in your case. It seems that due to different locales, the dates format are different.

I think the issue is with your switch statement for onClick based on the error. Start with taking that out by comment and see if you get still get the crash. If not then investigate the switch (v.getId()). I suspect that is returning less than the array you are expecting. Hope it helps a little!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM