Android Studio错误:E / AndroidRuntime

时间:2016-11-13 20:01:09

标签: java android

对于我的一个学校项目,我正在使用Android Studio构建自己的应用程序。这将是我的第一个应用程序。我也是Android Studio的新手,刚刚开始学习Java。我用python构建了一些游戏。

由于我是Java和应用程序开发的新手,因此该应用程序非常基础。它为用户提供了数学规则和公式,可用于研究测试等。

我已经对这个问题进行了研究,我注意到每个人都有不同的代码,这就是为什么我很想知道我是否可以得到一些关于代码问题的答案。

当我尝试运行我的应用时,我目前仍然遇到此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: mcshreker.learnthemalllta, PID: 2876
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{mcshreker.learnthemalllta/mcshreker.learnthemalllta.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' 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.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      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 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                      at mcshreker.learnthemalllta.MainActivity.onCreate(MainActivity.java:120)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      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) 

这是我的代码(MainActivity.Java):

package mcshreker.learnthemalllta;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import static mcshreker.learnthemalllta.R.layout.activity_main;
import static mcshreker.learnthemalllta.R.layout.activity_main3;
import static mcshreker.learnthemalllta.R.layout.content_main;

@SuppressWarnings("ALL")
public class MainActivity extends AppCompatActivity {

    private Button button;
    public Button button2;
    private Button button4;
    private Button button5;
    public Button button3;
    public Button button7;


    private void init6(){
        button = (Button)findViewById(R.id.button7);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy6 = new Intent(MainActivity.this,Main8Activity.class);
                startActivity(toy6);
            }
        });
    }

    private void init5(){
        button = (Button)findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy5 = new Intent(MainActivity.this,Main7Activity.class);
                startActivity(toy5);
            }
        });
    }

    private void init4(){
        button = (Button)findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy4 = new Intent(MainActivity.this,Main5Activity.class);
                startActivity(toy4);
            }
        });
    }

    private void init3(){
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy3 = new Intent(MainActivity.this,Main4Activity.class);
                startActivity(toy3);
            }
        });
    }



    private void init2(){
        button5 = (Button)findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy2 = new Intent(MainActivity.this,Main3Activity.class);

                startActivity(toy2);
            }
        });
    }

    private void init(){
        button4 = (Button)findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy = new Intent(MainActivity.this,Main2Activity.class);

                startActivity(toy);
            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        init();
        init2();
        init3();
        init4();
        init5();
        init6();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:1)

这可能不是确切的问题,但如果您确实阅读了错误,它会告诉您fab为空,这表明您可能删除了应用程序中存在的浮动操作按钮。

尝试删除这部分代码:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }
});