java - Android :Runtime exception 应用程序崩溃

标签 java android crash runtimeexception

我像 2 天前一样开始使用 Android,如果我犯了一个幼稚的错误,请原谅我。我正在用 Java 而不是 XML 制作界面,每次我在模拟器上启动它时应用程序都会崩溃。这是堆栈跟踪相同的:

03-01 21:08:46.941 7640-7640/com.example.prashant.wisdom E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.prashant.wisdom, PID: 7640
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prashant.wisdom/com.example.prashant.wisdom.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 com.example.prashant.wisdom.MainActivity.onCreate(MainActivity.java:65)
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) 
03-01 21:08:49.797 7640-7640/? I/Process: Sending signal. PID: 7640 SIG: 9

这是 MainActivity.java:
package com.example.prashant.wisdom;

import android.graphics.Color;
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.RelativeLayout;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RelativeLayout layout=new RelativeLayout(this); //layout
    Button button=new Button(this);         //button
    button.setText("SignIn");

    //giving ids to layout and button

    button.setId(1);
    layout.setId(2);

    //rules for button positioning
    RelativeLayout.LayoutParams buttonDetails=new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT

    );      //the above snippet is to be written for every widget in the activity.
            // For example,we'll write one for EditText also.
    buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
    buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);



    EditText username=new EditText(this);       //username input

    username.setId(3);

    RelativeLayout.LayoutParams usernameDetails=new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT

    );      //rules for the EditText details.
    //now we've to place the EditText above the button that is already positioned in the center.
    //the following snippet does that.

    usernameDetails.addRule(RelativeLayout.ABOVE,button.getId());
    usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);  //Rule for positioing in the center of the screen
    usernameDetails.setMargins(0,0,0,50); //padding on all the sides.We're adding padding just to the bottom
                                            //hence the rest are zero, but the bottom one is 50 pixels.
    layout.addView(username);             //EditText now positioned on the layout.


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    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();
        }
    });

    setContentView(layout);                     //placement of layout itself
    layout.addView(button, buttonDetails);      //placement of buttons on the layout





}

@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);
}
}

这里是 content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.prashant.wisdom.MainActivity"
tools:showIn="@layout/activity_main">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
</RelativeLayout>

最佳答案

你没有在你的 xml 中定义一个 FAB 所以

 findViewById(R.id.fab);

返回 null 并且您会得到一个简单的 NullpointerException 访问它

关于java - Android :Runtime exception 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35727598/

相关文章:

android - Gradle 看不到产品 flavor 。我怎样才能添加它们?

perl - Term::ReadLine::Gnu 中 “panic: free from wrong pool during global destruction.” 的原因是什么?

java - Map putAll 覆盖还是添加?

android - 如何在屏幕关闭时保持我的 Android 服务运行?

java - 如何使用 arquillian 测试依赖于持久性的 Rest-API

android - 如何使抽屉导航列表项消失,而不是不可见

c++ - Unicode 导致关闭消息框终止程序

c++ - 如何从崩溃中提取调试信息

java - 程序化 jetty 关闭

java - 改善月球车的运动