如何防止我的应用程序崩溃?

时间:2017-07-16 05:33:52

标签: android nullpointerexception

我是一个新手,并试图为在线投票制作一个Android应用程序。到目前为止我刚刚设计了布局,但遗憾的是,我面临渲染问题和空指针异常。

当我从MainActivity打开活动时,我只能看到白屏!活动顺序如下:

MainActivity --> Instructions or AdminLogin --> UserLogin or RegisterCandidate

即使经过数小时的谷歌搜索,我也无法找出确切的问题。我已经在StackOverflow本身上检查了几个答案,但未能弄清楚出了什么问题。

错误跟踪如下:

E/AndroidRuntime: FATAL EXCEPTION: main
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.palakjain.onlinevotingschool/com.example.palakjain.onlinevotingschool.AdminLogin}: java.lang.NullPointerException
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2360)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2412)
      at android.app.ActivityThread.access$600(ActivityThread.java:162)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
      at android.os.Handler.dispatchMessage(Handler.java:107)
      at android.os.Looper.loop(Looper.java:194)
      at android.app.ActivityThread.main(ActivityThread.java:5388)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:525)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
      at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.NullPointerException
      at com.example.suniljain.onlinevotingschool.AdminLogin.onCreate(AdminLogin.java:39)
      at android.app.Activity.performCreate(Activity.java:5141)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2412) 
      at android.app.ActivityThread.access$600(ActivityThread.java:162) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
      at android.os.Handler.dispatchMessage(Handler.java:107) 
      at android.os.Looper.loop(Looper.java:194) 
      at android.app.ActivityThread.main(ActivityThread.java:5388) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
      at dalvik.system.NativeStart.main(Native Method)

MainActivity代码:

public class MainActivity extends AppCompatActivity {

Button btnConductVoting, btnInstructions, btnSkipInstructions;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnConductVoting = (Button) findViewById(R.id.btnConductVoting);
    btnInstructions = (Button) findViewById(R.id.btnInstructions);
    btnSkipInstructions = (Button) findViewById(R.id.btnSkipInstructions);

    btnConductVoting.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, AdminLogin.class);
            startActivity(i);
        }
    });

    btnInstructions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, Instructions.class);
            startActivity(i);
        }
    });

    btnSkipInstructions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, UserLogin.class);
            startActivity(i);
        }
    });

}
}

说明代码:

public class Instructions extends AppCompatActivity{
Button btnStartVoting;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    btnStartVoting = (Button) findViewById(R.id.btnStartVoting);

    btnStartVoting.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Instructions.this, UserLogin.class);
            startActivity(i);
        }
    });
}
}

AdminLogin代码:

public class AdminLogin extends AppCompatActivity{

Button btnAdminLogin;
EditText etUsername;
EditText etPassword;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    btnAdminLogin = (Button) findViewById(R.id.btnAdminLogin);
    etUsername = (EditText) findViewById(R.id.etAdminPassword);
    etPassword = (EditText) findViewById(R.id.etAdminPassword);

    btnAdminLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(etUsername.getText().toString().equals("administrator") && etPassword.getText().toString().equals("password")){
                Intent i = new Intent(AdminLogin.this, RegisterCandidate.class);
                startActivity(i);
            }
        }
    });

}
}

UserLogin代码:

public class UserLogin extends AppCompatActivity{

//TextView tvUserForgotPassword;
TextView tvNewUserSignUp;
//Button btnUserLogIn;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    tvNewUserSignUp = (TextView) findViewById(R.id.tvNewUserSignUp);        

    tvNewUserSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(UserLogin.this, NewUser.class);
            startActivity(i);
        }
    });

}
}

RegisterCandidate代码:

public class RegisterCandidate extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
}

NewUser代码:

public class NewUser extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
}

Update1 :我已经完成了答案中建议的所有更改,但只要我点击MainActivity中的“跳过说明”按钮,我的应用程序就会崩溃。此外,当我从AdminLogin Activity中单击“登录”按钮时,应用程序崩溃。我该怎么办?

Update2 :我提到What is a NullPointerException, and how do I fix it?但无法解决错误!

4 个答案:

答案 0 :(得分:1)

Enable Source Control活动中没有setContentView

AdminLogin

只需注意,同样的问题与您的其他活动有关,请仔细检查。

如果您想知道什么是 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set contentview here before finding views otherwise they will be null btnAdminLogin = (Button) findViewById(R.id.btnAdminLogin); } ,那么在短期内它会关联在setContentView启动时显示的View / UI。例如 - 如果要在UI上显示ActivityEditText,则它们必须存在于扩展名为.xml的布局文件中。 (您也可以通过java代码创建View)

答案 1 :(得分:1)

您无法在管理登录活动==

中将字符串与etUsername.getText().toString() == "administrator"进行比较

使用

etUsername.getText().toString().equals("administrator")

你也没有像上面的回答中所述的setContentView

答案 2 :(得分:1)

至于你是Android上的新手,你必须阅读这些文件来设置setContentView

What is setContentView(R.layout.main)?

https://developer.android.com/training/basics/firstapp/building-ui.html

答案 3 :(得分:0)

Android没有后端和前端语言。两者都是使用Java完成的。 XML文件只是活动的布局或框架。要使用该活动,必须使用

# devtools::install_github("ropenscilabs/skimr")
dt %>% skimr::skim()
#> Numeric Variables
#> # A tibble: 2 x 13
#>     var    type missing complete     n       mean        sd       min
#>   <chr>   <chr>   <dbl>    <dbl> <dbl>      <dbl>     <dbl>     <dbl>
#> 1    X1 numeric       0       10    10  0.9626454 0.9001430 -0.798480
#> 2    X2 numeric       1        9    10 -0.1562312 0.8539957 -1.331122
#> # ... with 5 more variables: `25% quantile` <dbl>, median <dbl>, `75%
#> #   quantile` <dbl>, max <dbl>, hist <chr>