我是Android开发的新手。在我的活动中,我尝试使用捆绑和意图将一些字符串传递给下一个活动。但由于某种原因,应用程序崩溃并抛出错误:
致命例外:主要
按下按钮时的日志:
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 答案,但由于某种原因,它们似乎都不适合我。我的意图是不是做错了什么?提前谢谢。
问题是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);