[英]Application crashing in Android:Runtime exception
I started with Android like 2 days ago and please forgive me if I have made a naive error.I am making the interface in Java instead of XML and the application crashes every time I launch it on the emulator.Here is the stack trace for the same: 我两天前开始使用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
Here is MainActivity.java: 这是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);
}
}
And here is content_main.xml: 这是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>
You dont have a FAB defined in your xml so 您没有在XML中定义的FAB,因此
findViewById(R.id.fab);
returns null and you get a simple NullpointerException accessing it 返回null,您将获得一个简单的NullpointerException访问它
You have to call setContentView
before you can find views in your layout ( findViewById
). 您必须先调用setContentView
然后才能在布局中查找视图( findViewById
)。
Also it seems like none of your xml files is actually used in code, so nothing defined in any xml will be accessible. 同样,似乎您的xml文件中没有一个实际上在代码中使用,因此任何xml中定义的内容都无法访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.