android studio java.langNullPointerException null 对象引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28005428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android studio java.langNullPointerException null object reference
提问by Caedon Bates
I am continually getting an error when trying to run an android app telling me that there is a null
object reference. I am still a beginner with android and java and I would greatly appreciate any help I can get.
尝试运行 Android 应用程序时,我不断收到错误消息,告诉我存在null
对象引用。我仍然是 android 和 java 的初学者,如果我能得到任何帮助,我将不胜感激。
Here is the logcat
:
这是logcat
:
01-17 17:11:49.156 27778-27778/com.example.owner.sketchy D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
01-17 17:11:49.157 27778-27778/com.example.owner.sketchy E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.owner.sketchy, PID: 27778
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.owner.sketchy/com.example.owner.sketchy.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.getChildAt(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.getChildAt(int)' on a null object reference
at com.example.owner.sketchy.MainActivity.onCreate(MainActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
????????????at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
????????????at android.app.ActivityThread.access0(ActivityThread.java:144)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:135)
????????????at android.app.ActivityThread.main(ActivityThread.java:5221)
????????????at java.lang.reflect.Method.invoke(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:372)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
and here is my MainActivity.java
:
这是我的MainActivity.java
:
package com.example.owner.sketchy;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
private ImageButton currPaint;
private DrawingView drawView;
public void paintClicked(View view) {
if (view != currPaint) {
ImageButton imgView = (ImageButton) view;
String color = view.getTag().toString();
imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
currPaint = (ImageButton) view;
drawView.setColor(color);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
drawView = (DrawingView)findViewById(R.id.drawing);
LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
currPaint = (ImageButton)paintLayout.getChildAt(0);
}
@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);
}
}
回答by natario
Your paintLayout
is null, and your problem is onCreate()
.
When overriding methods from a super class, you should always (most of the times) call the method from that class using super.method()
, to allow normal processing.
你paintLayout
是空的,你的问题是onCreate()
. 当覆盖超类中的方法时,您应该始终(大多数情况下)使用super.method()
,调用该类中的方法,以允许正常处理。
Also, you are missing a call to setContentView(R.layout.your_layout)
. So don't forget these two lines:
此外,您错过了对setContentView(R.layout.your_layout)
. 所以不要忘记这两行:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
drawView = (DrawingView)findViewById(R.id.drawing);
LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
currPaint = (ImageButton)paintLayout.getChildAt(0);
}
回答by CommonsWare
Your onCreate()
method has two flaws:
你的onCreate()
方法有两个缺陷:
It does not chain to the superclass with
super.onCreate()
, which is typically the very first line in theonCreate()
method.It does not do anything to create a user interface, such as calling
setContentView()
.
它不使用 链接到超类
super.onCreate()
,这通常是onCreate()
方法中的第一行。它不会为创建用户界面做任何事情,例如调用
setContentView()
.
You are crashing because findViewById()
is returning null
, because you have no widgets yet.
您崩溃是因为findViewById()
正在返回null
,因为您还没有小部件。