android studio的新功能并获得空指针异常

时间:2018-09-29 19:04:01

标签: java android android-studio nullpointerexception

我的应用的主类

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

LogCat

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

4 个答案:

答案 0 :(得分:0)

您要尝试在声明后获取用户输入,因此始终为null,您需要在onclick回调及其可选项内移动String input = (String) Word.getText().toString();,但可以进行无效检查或try / catch以避免如果用户什么都不写,则返回空指针

答案 1 :(得分:0)

不需要(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");

答案 2 :(得分:0)

罗汉。 如果我对您的理解正确,则需要为TextWatcher使用EditText。 您收到NullPointerException,因为您的EditText为空。当editText中的文本更改时,TextWatcher会告诉您。

答案 3 :(得分:0)

只需使用它从EditText作为字符串获取文本

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