首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >android studio新手,并获得空指针异常

android studio新手,并获得空指针异常
EN

Stack Overflow用户
提问于 2018-09-30 03:04:01
回答 4查看 260关注 0票数 -3

我的应用程序的主类

代码语言:javascript
复制
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);
            }
        });
    }
}

我的应用程序的输出类

代码语言:javascript
复制
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);
            }
        });

    }
}

解释:此应用程序的目标是从输入页面获取用户的输入,然后将其传递到输出页面并打印出来。

The Problem:在几次尝试之后,我检查了日志,发现在我的应用程序的输出类中得到了一个空指针异常

代码语言:javascript
复制
String input = (String) Word.getText().toString();

LogCat

代码语言:javascript
复制
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
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-09-30 03:18:19

不需要(String)

代码语言:javascript
复制
String input = Word.getText().toString();

更新:

代码语言:javascript
复制
android.text.Editable android.widget.EditText.getText()' on a null object reference

它之所以获取null,是因为您试图从中获取字符串的EditText为空。怪不得。

另外,将字符串设置为前一个变量:

代码语言:javascript
复制
output.setText(input);

遵循以下命令:

onClick方法中使用以下代码:

代码语言:javascript
复制
String input = Word.getText().toString();
Intent intent = new Intent(youractivity.this, AnotherActivity.class);
intent.putExtra("YOUR_EXTRA", input);
startActivity(intent);

要获得另一个Activity,请在onCreate()中使用以下代码

代码语言:javascript
复制
String yourOutput = getIntent().getStringExtra("YOUR_EXTRA");
票数 0
EN

Stack Overflow用户

发布于 2018-09-30 03:15:31

您试图在声明之后获取用户输入,因此总是为空,您需要将String input = (String) Word.getText().toString();移动到onclick回调中,这是可选的,但如果用户没有编写任何内容,您可以设置空性检查或尝试/捕获以避免空指针

票数 0
EN

Stack Overflow用户

发布于 2018-09-30 03:19:14

罗翰。如果我没理解错的话,那么您的EditText需要使用TextWatcher。你得到了NullPointerException,因为你的EditText是空的。当editText中的文本发生更改时,TextWatcher会提示您。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52571591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档