Java 在设备上运行时,当转到主活动时,应用程序崩溃

Java 在设备上运行时,当转到主活动时,应用程序崩溃,java,android,android-studio,datetime,Java,Android,Android Studio,Datetime,所以这个应用程序在emulator上运行得很好,但是当我试图在设备上运行它时,它在主要活动上崩溃了。我需要帮助,我会非常感激的 以下是java的主要活动 package com.example.dashboard; import androidx.appcompat.app.AppCompatActivity; import androidx.cardview.widget.CardView; import android.content.Intent; import android.os.

所以这个应用程序在emulator上运行得很好,但是当我试图在设备上运行它时,它在主要活动上崩溃了。我需要帮助,我会非常感激的

以下是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;
        }

    }
}
和堆栈跟踪

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
我已经尝试删除年份。setTextsplitDate[2];记录2,一切正常。但我真的需要知道真正的问题是什么,因为前几个月和前几天的情况都很好


谢谢大家

打印出formattedDate,并确保在您的案例中正确选择分隔符逗号。似乎由于地区不同,日期格式也不同。

将日期格式设置为您可以控制的格式。例如:

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]);

我认为问题在于基于错误的onClick的switch语句。首先,通过评论将其删除,看看你是否仍然得到了崩溃。如果没有,则调查开关v.getId。我怀疑返回的数组少于您预期的数组。希望对你有点帮助

我可以知道如何确保我使用了正确的分隔符吗?@HusaynIrfan您可以强制使用下面答案中所示的日期格式,但现在仍然崩溃。您应该检查@S-sh的答案。根据我的说法,答案是正确的。