2015-01-17 1 views
1

null 객체 참조가 있음을 알리는 Android 앱을 실행할 때 계속 오류가 발생합니다. 나는 아직 안드로이드와 자바의 초보자이며, 내가 얻을 수있는 도움에 큰 감사를 표한다. 여기 android studio java.langNullPointerException null 객체 참조

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.access$800(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.access$800(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) 

여기 내 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); 
    } 

} 
+0

당신이 말하는 선형 레이아웃이 존재합니까? 그리고 그것은 아이보기가? –

+0

http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – user2864740

답변

3

paintLayout이 null이고 문제가 onCreate()입니다. 수퍼 클래스에서 메서드를 재정의하는 경우 정상 처리가 가능하도록 항상 (대부분의 경우) super.method()을 사용하여 해당 클래스의 메서드를 호출해야합니다.

또한 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); 
} 
+0

정말 고마워요! 내가 문제를 파악할 수 없었기 때문에 나는 매우 좌절감을 느꼈다. 그러나 이것은 효과가 있었다! 나는 자바/안드로이드에 대해 조금 더 배웠다는 것에 매우 감사하고 행복합니다! –

1

귀하의 onCreate() 방법은 두 가지 결함이 있습니다

  1. 그것은 체인하지 않는 슈퍼 클래스에를 super.onCreate()이며 일반적으로 onCreate() 메서드의 첫 번째 줄.

  2. setContentView()과 같이 사용자 인터페이스를 만드는 데는 아무 것도하지 않습니다.

당신이 아직 위젯이 없기 때문에 findViewById()null을 반환하기 때문에이 충돌하고 있습니다.

+0

맞아. 누구나 findViewById를 사용하기 전에 위젯이 있는지 확인하십시오. –

관련 문제