Android 通过 intent 传递包时应用程序崩溃

Android app crashes when passing through a bundle through intent

我是 Android 开发的新手。在我的 activity 中,我尝试使用 bundle 和 intent 传递一些字符串到下一个 activity。但由于某种原因,应用程序崩溃并引发错误:

FATAL EXCEPTION: main

按下按钮时的logcat:

05-20 15:42:27.137 15393-15393/com.me.backtrack.backtrack D/AndroidRuntime: Shutting down VM
05-20 15:42:27.138 15393-15393/com.me.backtrack.backtrack E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.me.backtrack.backtrack, PID: 15393
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
        at android.view.View.performClick(View.java:5637)
        at android.view.View$PerformClick.run(View.java:22429)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:5637) 
        at android.view.View$PerformClick.run(View.java:22429) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.me.backtrack.backtrack.CreateTrackActivity.onClickSave(CreateTrackActivity.java:62)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
        at android.view.View.performClick(View.java:5637) 
        at android.view.View$PerformClick.run(View.java:22429) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
05-20 15:42:27.142 1570-1843/system_process W/ActivityManager:   Force finishing activity com.me.backtrack.backtrack/.CreateTrackActivity
05-20 15:42:27.295 1292-1323/? D/AudioFlinger: mixer(0xb5b83c00) throttle end: throttle time(20)
05-20 15:42:27.390 1276-1999/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
05-20 15:42:27.409 1276-1999/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
05-20 15:42:27.411 1570-3402/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
05-20 15:42:27.411 1570-3402/system_process D/OpenGLRenderer: Swap behavior 1
05-20 15:42:27.423 1276-1999/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
05-20 15:42:27.567 1570-3402/system_process E/EGL_emulation: tid 3402: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
05-20 15:42:27.572 1570-3402/system_process W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9377b160, error=EGL_BAD_MATCH
05-20 15:42:27.666 1570-1586/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{f964620 u0 com.me.backtrack.backtrack/.CreateTrackActivity t104 f}
05-20 15:42:27.984 2514-16105/com.google.android.gms I/iu.UploadsManager: End new media; added: 0, uploading: 0, time: 215 ms

这里是开始 activity:

package com.me.backtrack.backtrack;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import static android.R.attr.id;
import static android.R.attr.name;
import static android.R.id.edit;
import static com.me.backtrack.backtrack.R.id.addTag;

public class CreateTrackActivity extends AppCompatActivity {

    private EditText trackNameView;
    Fragment fr;
    boolean count = false;
    private EditText TagViewName;

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

        trackNameView = (EditText) findViewById(R.id.trackName);
        final LinearLayout tagLayout = (LinearLayout)findViewById(R.id.tagView);

        final Button tagButton = (Button) findViewById(addTag);
        tagButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!count) {
                    Context c=CreateTrackActivity.this;
                    EditText tvv=new EditText(c);
                    tvv.setHint("Enter Tag");
                    tvv.setId(R.id.Tag_1);
                    tvv.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
                    tagButton.setVisibility(View.GONE);
                    tagLayout.addView(tvv);
                    count = true;
                }
            }
        });
        TagViewName = (EditText) findViewById(R.id.Tag_1);
    }

    public void onClickSave(View view) {
        String name = trackNameView.getText().toString();
        FileManager.CreateTrackDir(name);   //Creates track folders
        String tagName = TagViewName.getText().toString();
        Intent intent = new Intent(this, CameraActivity.class);

        //Bundle extras = intent.getExtras();
        Bundle bundle = new Bundle();
        bundle.putString("trackName", name); //pass through the folder name
        bundle.putString("tagName", tagName);
        intent.putExtra("bundle",bundle);
        startActivity(intent);
    }
}

这是第二个 activity 的 onCreate 部分:

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

    textureView = (TextureView) findViewById(R.id.camera_preview);
    assert textureView != null;
    textureView.setSurfaceTextureListener(textureListener);

    Bundle bundle = getIntent().getBundleExtra("bundle");
    nameOfTrack = bundle.getString("trackName");
    tagName = bundle.getString("tagName");
    //nameOfTrack = getIntent().getExtras().getString("trackName");
    //nameOfTrack = extras.getString("trackName");
    //tagName = extras.getString("tagName");
}

这 activity 大约 450 行代码,我希望这足以让您了解问题所在。但是,如果您希望我添加其余代码,请告诉我。我尝试使用另一个 Whosebug 答案,但似乎 none 出于某种原因对我有用。我的意图做错了吗?提前致谢。

问题是 TagViewName 是 Null。 问题来了

final Button tagButton = (Button) findViewById(addTag);
    tagButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!count) {
                Context c=CreateTrackActivity.this;
                EditText tvv=new EditText(c);
                tvv.setHint("Enter Tag");
                tvv.setId(R.id.Tag_1);
                tvv.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
                tagButton.setVisibility(View.GONE);
                tagLayout.addView(tvv);
                count = true;
            }
        }

    });
    TagViewName = (EditText) findViewById(R.id.Tag_1);

如你所见,首先通过id查找。一开始不存在的 id。稍后您添加布局并为其设置 id。

解决方法可以是:

 public void onClickSave(View view) {
    String name = trackNameView.getText().toString();
    FileManager.CreateTrackDir(name);   //Creates track folders
    TagViewName = (EditText) findViewById(R.id.Tag_1);
    String tagName = TagViewName.getText().toString();
    Intent intent = new Intent(this, CameraActivity.class);