0

我正在开发一个 android 应用程序,它需要我java.lang.NullPointerException,我不知道可能是什么原因。在以下几行中,您将获得logcat导致此崩溃的方法(第 151 行)。

04-17 18:56:19.729: E/AndroidRuntime(31291): FATAL EXCEPTION: main
04-17 18:56:19.729: E/AndroidRuntime(31291): java.lang.NullPointerException
04-17 18:56:19.729: E/AndroidRuntime(31291):    at tomorrowapps.com.tweeet.Main.DrawViewUp(Main.java:151)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at tomorrowapps.com.tweeet.Main.onOptionsItemSelected(Main.java:340)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.app.Activity.onMenuItemSelected(Activity.java:2548)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.view.View$PerformClick.run(View.java:17359)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.os.Handler.handleCallback(Handler.java:725)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.os.Looper.loop(Looper.java:137)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.app.ActivityThread.main(ActivityThread.java:5259)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at java.lang.reflect.Method.invoke(Method.java:511)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at dalvik.system.NativeStart.main(Native Method)

这里是导致异常的方法(第 151 行)

private void DrawViewUp() {
    // TODO Auto-generated method stub
    if(adview.getVisibility()==View.VISIBLE){
        drawview.bringToFront();                   //This is line 151
        adview.setVisibility(View.INVISIBLE);
        stroke.setVisibility(View.VISIBLE);
        rojo.setVisibility(View.VISIBLE);
        verde.setVisibility(View.VISIBLE);
        azul.setVisibility(View.VISIBLE);
        negro.setVisibility(View.VISIBLE);
    } else if (adview.getVisibility()==View.INVISIBLE){
        et1.bringToFront();
        adview.setVisibility(View.VISIBLE);
        stroke.setVisibility(View.INVISIBLE);
        rojo.setVisibility(View.INVISIBLE);
        verde.setVisibility(View.INVISIBLE);
        azul.setVisibility(View.INVISIBLE);
        negro.setVisibility(View.INVISIBLE);
    }
}

我还有一个方法,其中包含在 onCreate 方法上运行的所有 findViewById:

private void findViewById() {
    // TODO Auto-generated method stub
    et1=(EditText)findViewById(R.id.et1);
    tv1=(TextView)findViewById(R.id.tv1);
    views=(FrameLayout)findViewById(R.id.capture);
    drawview=(DrawView)findViewById(R.id.drawview);
    adview=(AdView)findViewById(R.id.adView);
    stroke=(SeekBar)findViewById(R.id.stroke);
    rojo=(ImageView)findViewById(R.id.rojo);
    verde=(ImageView)findViewById(R.id.verde);
    azul=(ImageView)findViewById(R.id.azul);
    negro=(ImageView)findViewById(R.id.negro);
}

所以有人知道为什么会崩溃吗?

编辑:我删除了我所有的代码,是不必要的

4

1 回答 1

1

问题是在 DrawView 类中,第二个和第三个构造函数在super()构造函数的其他变量中没有,如下例所示:

public class DrawView extends View {

    public DrawView(Context context) {
    super( context);
    }

    public DrawView(Context context, AttributeSet attrs) {
    super( context, attrs ); //first problem
    }

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
    super( context, attrs, defStyle ); //second problem
    }

}

使用解决:

findViewById() 为布局 XML 中的自定义组件返回 null,而不是其他组件

http://igoesolutions.com/blog/2011/06/20/custom-view-is-not-using-the-2-or-3-argument-view-constructors-xml-attributes-will-not-work/

于 2013-04-18T18:02:30.663 回答