0

我正在为视图使用四个不同的类,现在我想将四个类的对象组合在一个类中,然后想要实现 ontouchlistener,但它向我显示了错误。我不理解那个错误。我的代码如下:

public class AdActivity extends Activity {

    DemoView demoview ;
    DemoView2 finalview;
    DemoView3 dview;
    DemoView4 deview;
    LowerView lview;
    Finalview fview;

    ViewFlipper c,d ;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        demoview = new DemoView(this);
        finalview = new DemoView2(this);
        dview= new DemoView3(this);
        deview = new DemoView4(this);
        lview= new LowerView(this);
        fview=new Finalview(this);
        setContentView(fview);
    }

    public class Finalview extends View {
        public Finalview(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            c=(ViewFlipper)findViewById(R.id.viewFlipper2);
            c.addView(demoview,0);
            c.addView(finalview, 1);
            c.addView(dview, 2);
            c.addView(deview, 3);
            c.setAutoStart(true);
            c.setFlipInterval(1000);
            c.startFlipping();

            d=(ViewFlipper)findViewById(R.id.viewFlipper3);
            d.addView(lview,0);
        }
    } 
}

我的堆栈跟踪显示以下错误。我没有收到此错误。

05-07 15:28:56.900: E/AndroidRuntime(515): FATAL EXCEPTION: main
05-07 15:28:56.900: E/AndroidRuntime(515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ad/com.example.ad.AdActivity}: java.lang.NullPointerException
05-07 15:28:56.900: E/AndroidRuntime(515):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-07 15:28:56.900: E/AndroidRuntime(515):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-07 15:28:56.900: E/AndroidRuntime(515):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-07 15:28:56.900: E/AndroidRuntime(515):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-07 15:28:56.900: E/AndroidRuntime(515):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 15:28:56.900: E/AndroidRuntime(515):  at android.os.Looper.loop(Looper.java:123)
05-07 15:28:56.900: E/AndroidRuntime(515):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-07 15:28:56.900: E/AndroidRuntime(515):  at java.lang.reflect.Method.invokeNative(Native Method)
05-07 15:28:56.900: E/AndroidRuntime(515):  at java.lang.reflect.Method.invoke(Method.java:521)
05-07 15:28:56.900: E/AndroidRuntime(515):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-07 15:28:56.900: E/AndroidRuntime(515):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-07 15:28:56.900: E/AndroidRuntime(515):  at dalvik.system.NativeStart.main(Native Method)
05-07 15:28:56.900: E/AndroidRuntime(515): Caused by: java.lang.NullPointerException
05-07 15:28:56.900: E/AndroidRuntime(515):  at com.example.ad.AdActivity$Finalview.<init>(AdActivity.java:420)
05-07 15:28:56.900: E/AndroidRuntime(515):  at com.example.ad.AdActivity.onCreate(AdActivity.java:44)
05-07 15:28:56.900: E/AndroidRuntime(515):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-07 15:28:56.900: E/AndroidRuntime(515):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-07 15:28:56.900: E/AndroidRuntime(515):  ... 11 more
4

1 回答 1

0

堆栈跟踪的重要部分是:

05-07 15:28:56.900: E/AndroidRuntime(515): Caused by: java.lang.NullPointerException
05-07 15:28:56.900: E/AndroidRuntime(515):  at com.example.ad.AdActivity$Finalview.<init>(AdActivity.java:420)
05-07 15:28:56.900: E/AndroidRuntime(515):  at com.example.ad.AdActivity.onCreate(AdActivity.java:44)

它表示通过调用 findViewByID() 将 FinalView 构造函数中的“c”或“d”设置为 null。

您确定 R.id.viewFlipper2 和 R.id.viewFlipper3 实际上是在创建 AdActivity 时自行构建的吗?这些部分是否是此活动的 UI XML 描述?

于 2012-05-07T21:09:51.083 回答