我的应用程序在执行时抛出异常

时间:2016-07-01 16:55:52

标签: android fatal-error runtimeexception

这是我的logcat:

  

07-01 17:51:51.190 14519-14519 /?我/艺术:不迟到-Xcheck:jni   (已经开启)07-01 17:51:51.290 14519-14519 /? d / AndroidRuntime:   关闭VM 07-01 17:51:51.290 14519-14519 /? E / AndroidRuntime:   致命异议:主要                                                      过程:com.exampl.home.assignmentques5,PID:14519                                                      java.lang.RuntimeException:无法实例化活动   ComponentInfo {com.exampl.home.assignmentques5 / com.exampl.home.assignmentques5.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法   'android.text.Editable android.widget.EditText.getText()'上的null   对象参考                                                          在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)                                                          在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)                                                          在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                                                          在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                          在android.os.Looper.loop(Looper.java:135)                                                          在android.app.ActivityThread.main(ActivityThread.java:5254)                                                          at java.lang.reflect.Method.invoke(Native Method)                                                          在java.lang.reflect.Method.invoke(Method.java:372)                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)                                                       引起:java.lang.NullPointerException:尝试调用虚方法   'android.text.Editable android.widget.EditText.getText()'上的null   对象参考                                                          在com.exampl.home.assignmentques5.MainActivity。(MainActivity.java:24)                                                          at java.lang.reflect.Constructor.newInstance(Native Method)                                                          在java.lang.Class.newInstance(Class.java:1606)                                                          在android.app.Instrumentation.newActivity(Instrumentation.java:1066)                                                          在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)                                                          在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)                                                          在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                                                          在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                          在android.os.Looper.loop(Looper.java:135)                                                          在android.app.ActivityThread.main(ActivityThread.java:5254)                                                          at java.lang.reflect.Method.invoke(Native Method)                                                          在java.lang.reflect.Method.invoke(Method.java:372)                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)07-01   17:52:00.390 14519-14526 / com.exampl.home.assignmentques5 W / art:   暂停所有线程:180ms 07-01 17:56:51.770 14519-14519 /?   I /处理:发送信号。 PID:14519 SIG:9

这是我的Java代码:

package com.exampl.home.assignmentques5;

{
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    Button b1,b2;
    EditText et1,et2,et3,et4,et5,et6,et7;
    TextView tv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button)findViewById(R.id.button);
        b2=(Button)findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        tv1=(TextView)findViewById(R.id.textView4);
    }
    String s1 = et1.getText().toString();
    String s2 = et2.getText().toString();
    String s3 = et3.getText().toString();
    String s4 = et4.getText().toString();
    String s5 = et5.getText().toString();
    String s6 = et6.getText().toString();
    String s7 = et7.getText().toString();

    int a = Integer.parseInt(s7);

    @Override
    public void onClick(View v)
    { 
        if(v.getId()==R.id.button)
        {
            String S = "Your name is "+s1+" "+s2+" "+s3+"\nYour Address is"+et4+" "+et5+" "+et6+" "+a;
            tv1.setText(S);
        }
        else
        {   et1.setText(" ");
            et2.setText(" ");
            et3.setText(" ");
            et4.setText(" ");
            et5.setText(" ");
            et6.setText(" ");
            et7.setText(" ");

        }

    }
}

2 个答案:

答案 0 :(得分:0)

所有这些对象:

EditText et1,et2,et3,et4,et5,et6,et7;

未在onCreated中初始化

所以你试图得到一个空引用 EditText 的文本 在这之前: (最好将其移到方法内部)

String s1  ;
String s2  ;
String s3  ;
....

在onCreate方法

中初始化Wodge
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1=(Button)findViewById(R.id.button);
    b2=(Button)findViewById(R.id.button2);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    tv1=(TextView)findViewById(R.id.textView4);


    //
    et1 = (EditText )findViewById(R.id.????);
    et2 = (EditText )findViewById(R.id.????);
    et3 = (EditText )findViewById(R.id.????);
    ...
   initStrings();
}
private void initStrings(){
  s1 = et1.getText().toString();
  s2 = et2.getText().toString();
  s3 = et3.getText().toString();
  s4 = et4.getText().toString();
  s5 = et5.getText().toString();
  s6 = et6.getText().toString();
  s7 = et7.getText().toString();
}

答案 1 :(得分:0)

这是因为您在getText()EditText尝试MainActivity时创建类et1, et2, et3时为null。您应该在使用findView方法时使用onCreate初始化getText()。只有这样你才能<MyMessage>Meeesaaagee</MyMessage> 来自他们。