Android应用程序在通过意图传递捆绑包时崩溃

时间:2017-05-20 19:52:41

标签: android android-intent nullpointerexception android-bundle

我对Android开发有点新意。在我的活动中,我试图使用bundle和意图传递一些字符串到下一个活动。但由于某种原因,应用程序崩溃并抛出错误:

  

致命的例外:主要

按下按钮时的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

以下是开始活动:

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

以下是第二项活动的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");
}

这项活动就像450行代码一样,我希望这足以让我们知道这个问题。但如果您希望我添加其余代码,请告诉我。我尝试使用另一个StackOverflow答案,但似乎没有一个因为某些原因而对我有效。我对意图做错了吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

问题是TagViewName是空的。 这是问题

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