发送意图到其他活动时发生错误。

huangapple go评论76阅读模式
英文:

Error while sending intent to other activity

问题

This code appears to be written in Java for an Android application. It consists of two activities: MainActivity and Testing2. The primary issue seems to be related to a "TransactionTooLargeException" that occurs when trying to start an activity. This exception typically arises when the data being passed between activities exceeds the allowed size.

Without the full context of your project and layout files, it's challenging to pinpoint the exact issue. However, you may want to consider the following:

  1. Check the size of data being passed between activities via Intent extras (e.g., hisnumber and hisimages). If these lists become too large, it can trigger the "TransactionTooLargeException."

  2. Review the layout XML files associated with MainActivity and Testing2 to ensure that the UI elements and button IDs match the code.

  3. Verify that both MainActivity and Testing2 are correctly declared in your AndroidManifest.xml file.

  4. Ensure that the necessary permissions are declared in the manifest if your app requires any specific permissions.

  5. Look for any other runtime errors or issues in your code that might be causing unexpected behavior.

Remember to address the data size issue first, as it appears to be the immediate problem causing the crash. You may need to rethink how you pass and handle large amounts of data between activities in your Android app.

英文:

Whats wrong in this code? It is not sending the intent to next page.I have done it many times.When i click on the button on the Mainactivity.java i get this log.It doesn't go to the next page.Intent is not working in this activity why?Hope to get answer soon.Thanks in advance.

package com.example.testing1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<String> hisnumber=new ArrayList<>();
    ArrayList<Bitmap> hisimages=new ArrayList<>();
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        Intent intent = getIntent();
        hisnumber = intent.getStringArrayListExtra("hisnumber");
        hisimages = intent.getParcelableArrayListExtra("hisimages");
        Log.d("num1", String.valueOf(hisnumber));
        Log.d("img1", String.valueOf(hisimages));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(MainActivity.this, Testing2.class);
                myIntent.putExtra("hisnumber", hisnumber);
                myIntent.putParcelableArrayListExtra("hisimages", hisimages);
                Log.d("num", String.valueOf(hisnumber));
                Log.d("img", String.valueOf(hisimages));
                startActivity(myIntent);
            }
        });
    }
}

Mainactivity.java

package com.example.testing1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class Testing2 extends AppCompatActivity {
    ArrayList<String> hisnumber=new ArrayList<>();
    ArrayList<Bitmap> hisimages=new ArrayList<>();
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testing2);
        button=findViewById(R.id.button);
        Intent intent = getIntent();
        hisnumber = intent.getStringArrayListExtra("hisnumber");
        hisimages = intent.getParcelableArrayListExtra("hisimages");
        Log.d("num3", String.valueOf(hisnumber));
        Log.d("img3", String.valueOf(hisimages));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(hisnumber==null){
                    hisnumber=new ArrayList<>();
                    hisimages=new ArrayList<>();
                }
                hisnumber.add("ass");
                Bitmap bp= BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);
                hisimages.add(bp);
                Intent myIntent = new Intent(Testing2.this, MainActivity.class);
                myIntent.putExtra("hisnumber", hisnumber);
                myIntent.putParcelableArrayListExtra("hisimages", hisimages);
                Log.d("num2", String.valueOf(hisnumber));
                Log.d("img2", String.valueOf(hisimages));
                startActivity(myIntent);
            }
        });
    }
}

Testing.java


07/28 11:07:11: Launching 'app' on Realme RMX1901.
$ adb shell am start -n "com.example.testing1/com.example.testing1.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 18360 on device 'realme-rmx1901-618cb732'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
E/Perf: Fail to get file list com.example.testing1
    getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
    Fail to get file list com.example.testing1
    getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
W/xample.testing: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/xample.testing: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
D/num1: null
D/img1: null
D/WindowManager: Add to mViews: DecorView@5a6d981[MainActivity], this = android.view.WindowManagerGlobal@9161a26,pkg= com.example.testing1
I/AdrenoGLES: QUALCOMM build                   : e4029f9, I6b4cbc7431
    Build Date                       : 10/01/19
    OpenGL ES Shader Compiler Version: EV031.27.05.01
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
I/AdrenoGLES: Build Config                     : S P 8.0.11 AArch64
I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
W/AdrenoUtils: <ReadGpuID_from_sysfs:194>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model
    <ReadGpuID:218>: Failed to read chip ID from gpu_model. Fallback to use the GSL path
W/Gralloc3: mapper 3.x is not supported
I/Choreographer: Skipped 9 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 1 frames!  The application may be doing too much work on its main thread.
D/ColorViewRootUtil: nav gesture mode swipeFromBottom ignore false downY 349 mScreenHeight 2340 mScreenWidth 1080 mStatusBarHeight 54 globalScale 1.125 nav mode 3 event MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=334.0, y[0]=349.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=660935273, downTime=660935273, deviceId=7, source=0x1002, displayId=0 } rotation 0
D/num: null
D/img: null
I/Choreographer: Skipped 1 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 1 frames!  The application may be doing too much work on its main thread.
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@fa02c62
D/num3: null
D/img3: null
D/WindowManager: Add to mViews: DecorView@76b323c[Testing2], this = android.view.WindowManagerGlobal@9161a26,pkg= com.example.testing1
I/Choreographer: Skipped 6 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 3 frames!  The application may be doing too much work on its main thread.
I/xample.testing: ProcessProfilingInfo new_methods=681 is saved saved_to_disk=1 resolve_classes_delay=5000
D/ColorViewRootUtil: nav gesture mode swipeFromBottom ignore false downY 327 mScreenHeight 2340 mScreenWidth 1080 mStatusBarHeight 54 globalScale 1.125 nav mode 3 event MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=81.0, y[0]=327.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=660939314, downTime=660939314, deviceId=7, source=0x1002, displayId=0 } rotation 0
I/Choreographer: Skipped 1 frames!  The application may be doing too much work on its main thread.
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 3240640)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testing1, PID: 8832
    java.lang.RuntimeException: Failure from system
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1720)
        at android.app.Activity.startActivityForResult(Activity.java:5319)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:5263)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at android.app.Activity.startActivity(Activity.java:5648)
        at android.app.Activity.startActivity(Activity.java:5616)
        at com.example.testing1.Testing2$1.onClick(Testing2.java:50)
        at android.view.View.performClick(View.java:7256)
        at android.view.View.performClickInternal(View.java:7218)
        at android.view.View.access$3800(View.java:824)
        at android.view.View$PerformClick.run(View.java:27719)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:228)
        at android.app.ActivityThread.main(ActivityThread.java:7782)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)
     Caused by: android.os.TransactionTooLargeException: data parcel size 3240640 bytes
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(BinderProxy.java:523)
        at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3868)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1714)
        at android.app.Activity.startActivityForResult(Activity.java:5319) 
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676) 
        at android.app.Activity.startActivityForResult(Activity.java:5263) 
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663) 
        at android.app.Activity.startActivity(Activity.java:5648) 
        at android.app.Activity.startActivity(Activity.java:5616) 
        at com.example.testing1.Testing2$1.onClick(Testing2.java:50) 
        at android.view.View.performClick(View.java:7256) 
        at android.view.View.performClickInternal(View.java:7218) 
        at android.view.View.access$3800(View.java:824) 
        at android.view.View$PerformClick.run(View.java:27719) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:228) 
        at android.app.ActivityThread.main(ActivityThread.java:7782) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981) 
I/Process: Sending signal. PID: 8832 SIG: 9


Log

答案1

得分: 1

主要原因是NPE是这样的:

hismumber = intent.getStringArrayListExtra("hisnumber");
hisimages = intent.getParcelableArrayListExtra("hisimages");

需要检查为什么这不起作用

NPE的解决方法

ArrayList 似乎为null,所以在 onClick() 中检查是否为null:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {   
        Intent myIntent = new Intent(Testing2.this, MainActivity.class);
        if(hisnumber != null) {
            myIntent.putExtra("hisnumber", hisnumber);
        }
        if(hisimages != null) {
            hisnumber.add("ass");
            Bitmap bp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);
            hisimages.add(bp);
            myIntent.putParcelableArrayListExtra("hisimages", hisimages);
            Log.d("num2", String.valueOf(hisnumber));
            Log.d("img2", String.valueOf(hisimages));
        }  
        startActivity(myIntent);
    }
});
英文:

Main reason behind NPE is this

hisnumber = intent.getStringArrayListExtra("hisnumber");
        hisimages = intent.getParcelableArrayListExtra("hisimages");

You need to check why is this not working

Workaround for NPE

Arraylist seems to be null , so check if its null or not in onClick()

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {   
               Intent myIntent = new Intent(Testing2.this, MainActivity.class);
               if(hisnumber!=null){
               myIntent.putExtra("hisnumber", hisnumber);
               }
                if(hisimages!=null){
                hisnumber.add("ass");
                Bitmap bp= BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);
                hisimages.add(bp);
                myIntent.putParcelableArrayListExtra("hisimages", hisimages);
                Log.d("num2", String.valueOf(hisnumber));
                Log.d("img2", String.valueOf(hisimages));
                }  
               
                startActivity(myIntent);
            }
        });

答案2

得分: 0

Your hisnumberhisimages 列表为空
在添加之前检查它们的可空性...

Testing.java

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (hisnumber != null) {
            hisnumber.add("ass");
        }
        Bitmap bp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);
        if (hisimages != null) {
            hisimages.add(bp);
        }
        Intent myIntent = new Intent(Testing2.this, MainActivity.class);
        myIntent.putExtra("hisnumber", hisnumber);
        myIntent.putParcelableArrayListExtra("hisimages", hisimages);
        Log.d("num2", String.valueOf(hisnumber));
        Log.d("img2", String.valueOf(hisimages));
        startActivity(myIntent);
    }
});
英文:

Your hisnumberand hisimages lists are NULL
Check their Nullability before adding somthing..

Testing.java

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (hisnumber != null) {
                hisnumber.add("ass");
                 }
                Bitmap bp= BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);
               if (hisimages != null) {
                hisimages.add(bp);
                 }
                Intent myIntent = new Intent(Testing2.this, MainActivity.class);
                myIntent.putExtra("hisnumber", hisnumber);
                myIntent.putParcelableArrayListExtra("hisimages", hisimages);
                Log.d("num2", String.valueOf(hisnumber));
                Log.d("img2", String.valueOf(hisimages));
                startActivity(myIntent);
            }
        });

huangapple
  • 本文由 发表于 2020年7月28日 13:35:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63127657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定