安卓工作室的新手,并得到一个空指针异常



我的应用程序的主类

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("button","onCreate: Starting");
Button btn = (Button) findViewById(R.id.GoNextScreen);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("button","onCreate: pressed");
Intent intent = new Intent(MainActivity.this,OutputResult.class);
startActivity(intent);
}
});
}
}

我的应用的输出类

package newapptry.com.permutation;
import android.content.Intent;
import android.icu.util.Output;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OutputResult extends AppCompatActivity   {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.output_result);
EditText Word = (EditText) findViewById(R.id.InputWord);
TextView output = (TextView) findViewById(R.id.OutputTextView);
String input = (String) Word.getText().toString();
output.setText(input);
Log.d("button","onCreate: Starting");
Button back = (Button) findViewById(R.id.BackButton);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("button","onCreate: pressed!");
Intent ins = new Intent(OutputResult.this,MainActivity.class);
startActivity(ins);
}
});
}
}

说明:此应用的目标是从输入页获取用户的输入,然后将其传递到输出页并打印。

问题:经过几次尝试,我检查了日志,发现我在应用程序的输出类中收到空指针异常

String input = (String) Word.getText().toString();

日志猫

09-29 15:32:42.992 17233-17233/? I/art: Late-enabling -Xcheck:jni
09-29 15:32:43.132 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.422 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.492 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.572 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.642 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.722 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.812 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.882 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.962 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:44.032 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:44.102 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:44.122 17233-17233/newapptry.com.permutation W/System: ClassLoader referenced unknown path: /data/app/newapptry.com.permutation-1/lib/arm
09-29 15:32:44.122 17233-17233/newapptry.com.permutation I/InstantRun: starting instant run server: is main process
09-29 15:32:44.272 17233-17233/newapptry.com.permutation W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@9542280
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@a55e0b9
09-29 15:32:44.392 17233-17233/newapptry.com.permutation I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
09-29 15:32:44.392 17233-17233/newapptry.com.permutation I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
09-29 15:32:44.522 17233-17233/newapptry.com.permutation D/button: onCreate: Starting
09-29 15:32:44.552 17233-17277/newapptry.com.permutation D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-29 15:32:44.602 17233-17277/newapptry.com.permutation I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8909_LA.BR.1.2.6_RB1__release_AU (Ib683e2651b)
OpenGL ES Shader Compiler Version: E031.29.00.00
Build Date: 03/30/16 Wed
Local Branch: 
Remote Branch: quic/LA.BR.1.2.6_rb1.12
Local Patches: NONE
Reconstruct Branch: NOTHING
09-29 15:32:44.602 17233-17277/newapptry.com.permutation I/OpenGLRenderer: Initialized EGL, version 1.4
09-29 15:32:44.702 17233-17233/newapptry.com.permutation W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
09-29 15:32:50.602 17233-17233/newapptry.com.permutation D/Settings: start to get string for name : sound_effects_enabled , userHandler : 0
09-29 15:32:50.602 17233-17233/newapptry.com.permutation V/Settings: invalidate [system]: current 10 != cached 0
09-29 15:32:50.602 17233-17233/newapptry.com.permutation D/Settings: start to get string with lazy provider
09-29 15:32:50.612 17233-17233/newapptry.com.permutation D/Settings: return value in bundle is not null
09-29 15:32:50.612 17233-17233/newapptry.com.permutation D/button: onCreate: pressed
09-29 15:32:50.682 17233-17233/newapptry.com.permutation V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@7a54e01
09-29 15:32:50.682 17233-17233/newapptry.com.permutation V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@4ecb4a6
09-29 15:32:50.702 17233-17233/newapptry.com.permutation D/AndroidRuntime: Shutting down VM
09-29 15:32:50.702 17233-17233/newapptry.com.permutation E/AndroidRuntime: FATAL EXCEPTION: main
Process: newapptry.com.permutation, PID: 17233
java.lang.RuntimeException: Unable to start activity ComponentInfo{newapptry.com.permutation/newapptry.com.permutation.OutputResult}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at newapptry.com.permutation.OutputResult.onCreate(OutputResult.java:25)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.access$900(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:171) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
09-29 15:32:52.122 17233-17233/newapptry.com.permutation I/Process: Sending signal. PID: 17233 SIG: 9

无需(String)

String input = Word.getText().toString();

更新:

android.text.Editable android.widget.EditText.getText()' on a null object reference

它变得null,因为您尝试从中获取字符串的EditText是空的。这就是原因。

此外,您将字符串设置为上一个变量:

output.setText(input);

遵循这个:

onClick方法中使用此方法:

String input = Word.getText().toString();
Intent intent = new Intent(youractivity.this, AnotherActivity.class);
intent.putExtra("YOUR_EXTRA", input);
startActivity(intent);

要进入另一个Activity,请在onCreate()内使用以下代码:

String yourOutput = getIntent().getStringExtra("YOUR_EXTRA");

您正在尝试在声明后获取用户输入,因此全部为空,您需要在 onclick 回调中移动String input = (String) Word.getText().toString();,并且它是可选的,但是如果用户不写任何东西,您可以进行空性检查或 try/catch 以避免空指针

Rohan。 如果我理解正确,那么您需要使用TextWatcher进行EditText。 你得到了 NullPointerException,因为你的 EditText 是空的。当编辑文本中的文本更改时,TextWatcher 会说出您。

只需使用它即可从 EditText 中获取字符串形式的文本

output.setText(Word.getText().toString());

相关内容

最新更新