Android中的应用程序崩溃:运行时异常

时间:2016-03-01 16:00:47

标签: 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>

2 个答案:

答案 0 :(得分:0)

您的xml中没有定义FAB,所以

 findViewById(R.id.fab);

返回null,你会得到一个简单的NullpointerException来访问它

答案 1 :(得分:0)

您必须先致电{ "config": { "log": "debug" }, "extractor" : { "jdbc": { "driver": "com.mysql.jdbc.Driver", "url": "jdbc:mysql://localhost:3306/mysqldb", "userName": "root", "userPassword": "", "query": "select * from TableB" } }, "transformers" : [ { "vertex": { "class": "TableB"} }, { "edge": { "class": "associate", "direction" : "in", "joinFieldName": "idTableB", "lookup":"TableA.idTableA" } } ], "loader" : { "orientdb": { "dbURL": "plocal:/temp/databases/orientdb", "dbAutoCreate": true } } } ,然后才能在布局中找到视图(setContentView)。

此外,似乎没有任何xml文件实际用于代码,因此任何xml中定义的内容都不可访问。