一致的OnClickListener问题

时间:2018-03-01 11:10:24

标签: java android nullpointerexception onclicklistener

运行MaintenanceActvity时应用程序不断崩溃。 LogCat指向代码第60行的NullPointerException。但是,从类似的活动中回收代码后,我似乎无法弄清楚为什么这似乎是一个问题。

我很感激在这个方向上向正确的方向点头,因为它已经让我烦恼了一天多了。我认为这是一个相对简单的修复方法,但唉,我还没有找到解决方案。

我已检查过此类LogCat错误的其他SO主题,但无法找到适合我的解决方案。

logcat的

03-01 11:00:03.619 5705-5705/? E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.***.myapplication/com.example.***.myapplication.MaintenanceActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                    at android.os.Handler.dispatchMessage(Handler.java:106)
                                                    at android.os.Looper.loop(Looper.java:164)
                                                    at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                    at com.example.***.myapplication.MaintenanceActivity.onCreate(MaintenanceActivity.java:60)
                                                    at android.app.Activity.performCreate(Activity.java:7009)
                                                    at android.app.Activity.performCreate(Activity.java:7000)
                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                    at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                    at android.os.Looper.loop(Looper.java:164) 
                                                    at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

OnClickLister(从@ Line 60开始)

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

    databaseMaintenance = FirebaseDatabase.getInstance().getReference("maintenance");

    editTextTitle = (EditText) findViewById(R.id.editTextTitle);
    editTextDesc = (EditText) findViewById(R.id.editTextDesc);
    buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
    spinnerPrimary = (Spinner) findViewById(R.id.spinnerPrimary);
    spinnerSecondary = (Spinner) findViewById(R.id.spinnerSecondary);
    spinnerProperty = (Spinner) findViewById(R.id.spinnerProperty);

    listViewIssues = (ListView) findViewById(R.id.listViewIssues);

    maintenanceList = new ArrayList<>();

    buttonSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            addMaintenance();
        }
    });

addMaintenance()方法

    private void addMaintenance(){

    String title = editTextTitle.getText().toString().trim();
    String desc = editTextDesc.getText().toString().trim();
    String primary = spinnerPrimary.getSelectedItem().toString();
    String secondary = spinnerSecondary.getSelectedItem().toString();
    String property = spinnerProperty.getSelectedItem().toString();

    if(!TextUtils.isEmpty(title)){

        String id = databaseMaintenance.push().getKey();

        Maintenance maintenance = new Maintenance (id, title, desc, primary, secondary, property);

        databaseMaintenance.child(id).setValue(maintenance);

        Toast.makeText(this, "Maintenance Added", Toast.LENGTH_LONG).show();

    } else {

        Toast.makeText(this, "You must enter a maintenance record", Toast.LENGTH_LONG).show();
    }

}

3 个答案:

答案 0 :(得分:0)

可能是您向按钮

提供了错误的ID
buttonSubmit=(Button)findViewById(R.id.buttonSubmit);

检查xml文件中按钮的ID。

答案 1 :(得分:0)

检查您在活动中使用的xml文件,然后检查您的按钮的ID 然后检查你的活动 XML按钮ID&amp;活动中的按钮初始化ID应该相同。

答案 2 :(得分:0)

在所提供的一些答案的帮助下,事实证明Android Studio已复制activity_maintenance.xml文件以在侧栏中制作v16版本。我只需删除这两个文件并拥有一个activity_maintenance.xml文件。

谢谢大家。