簡體   English   中英

android studio 新手,遇到空指針異常

[英]New to android studio and getting a null pointer exception

我的應用程序的主類

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 input = (String) Word.getText().toString(); 在 onclick 回調中,它是可選的,但是如果用戶不寫任何內容,您可以進行空值檢查或 try/catch 以避免空指針

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

羅漢。 如果我理解正確,那么您需要為 EditText 使用TextWatcher 你得到 NullPointerException,因為你的 EditText 是空的。 當 editText 中的文本更改時,TextWatcher 會告訴您。

只需使用它從 EditText 獲取文本作為字符串

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM