0

我通过调用其他类在活动中显示图像,但它没有显示它向我显示错误的图像。我的代码如下

public class VideosActivity extends Activity {
    //DrawView draw;
    float a=0;
    float b=0;
    ImageView image;
    MotionEvent event;
    Button  back ;
    Button next;
    TextView t;
    String info = "";
    int count =0;
    FirstImage  i= new FirstImage (this);
TableLayout tl;
TableRow ru,rm,rl,rlm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.videos_layout);
         i=(FirstImage)findViewById(R.id.first);
         //back = (Button) findViewById(R.id.button1);
        // next = (Button) findViewById(R.id.button2);
        //
     // addListenerOnButton();


}



    private void addListenerOnButton() {
        // TODO Auto-generated method stub

        if (count == 0)
            back.setEnabled(false);

        next.setOnClickListener( new OnClickListener() {


            public void onClick(View arg0) {
                if(count==0){
                image.setImageResource(R.drawable.hand);
                back.setEnabled(true);
                count++;

                addListenerOnButton();

                }
                else if (count==1){
                    image.setImageResource(R.drawable.tissue);
                    count++;

                    addListenerOnButton();

                }

                else if (count==2){
                    image.setImageResource(R.drawable.cellconstfinal1);
                    count++;

                    addListenerOnButton();


                }
                else if (count==3){
                    //nucleus
                    image.setImageResource(R.drawable.cellconstfinal1);
                    count++;

                    addListenerOnButton();

                }
                else if (count==4){
                    //nchromosome
                    image.setImageResource(R.drawable.hand);
                    count++;

                    addListenerOnButton();

                }
                else
                {   //double helix
                    count++;
                    image.setImageResource(R.drawable.handash1);
                    next.setEnabled(false);

                    addListenerOnButton();

                }

            }
        });

        back.setOnClickListener(new OnClickListener() {


            public void onClick(View arg0) {
                if(count==1){
                    image.setImageResource(R.drawable.human);
                    back.setEnabled(false);
                    count--;

                    addListenerOnButton();

                    }
                    else if (count==2){
                        image.setImageResource(R.drawable.hand);
                        count--;

                        addListenerOnButton();

                    }

                    else if (count==3){
                        image.setImageResource(R.drawable.tissue);
                        count--;

                        addListenerOnButton();


                    }
                    else if (count==4){
                        image.setImageResource(R.drawable.cellconstfinal1);
                        count--;

                        addListenerOnButton();

                    }
                    else if (count==5){
                        //nucleus
                        image.setImageResource(R.drawable.tissue);
                        count--;

                        addListenerOnButton();


                    }
                    else {
                        //chromosome
                        //count==6
                        image.setImageResource(R.drawable.cellconstfinal1);
                        count--;

                        addListenerOnButton();
                        next.setEnabled(true);

                    }
                }
            });
    }

它向我显示以下错误。我不明白为什么它向我显示以下错误。

05-10 19:27:32.097: E/AndroidRuntime(638): FATAL EXCEPTION: main
05-10 19:27:32.097: E/AndroidRuntime(638): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidtablayout/com.example.androidtablayout.VideosActivity}: java.lang.NullPointerException
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.view.View.performClick(View.java:2408)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.view.View$PerformClick.run(View.java:8816)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.os.Handler.handleCallback(Handler.java:587)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.os.Looper.loop(Looper.java:123)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-10 19:27:32.097: E/AndroidRuntime(638):  at java.lang.reflect.Method.invokeNative(Native Method)
05-10 19:27:32.097: E/AndroidRuntime(638):  at java.lang.reflect.Method.invoke(Method.java:521)
05-10 19:27:32.097: E/AndroidRuntime(638):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-10 19:27:32.097: E/AndroidRuntime(638):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-10 19:27:32.097: E/AndroidRuntime(638):  at dalvik.system.NativeStart.main(Native Method)
05-10 19:27:32.097: E/AndroidRuntime(638): Caused by: java.lang.NullPointerException
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.view.View.<init>(View.java:1810)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.widget.ImageView.<init>(ImageView.java:103)
05-10 19:27:32.097: E/AndroidRuntime(638):  at com.example.androidtablayout.FirstImage.<init>(FirstImage.java:8)
05-10 19:27:32.097: E/AndroidRuntime(638):  at com.example.androidtablayout.VideosActivity.<init>(VideosActivity.java:27)
05-10 19:27:32.097: E/AndroidRuntime(638):  at java.lang.Class.newInstanceImpl(Native Method)
05-10 19:27:32.097: E/AndroidRuntime(638):  at java.lang.Class.newInstance(Class.java:1429)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-10 19:27:32.097: E/AndroidRuntime(638):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-10 19:27:32.097: E/AndroidRuntime(638):  ... 18 more
05-10 19:27:41.897: I/Process(638): Sending signal. PID: 638 SIG: 9

第一个图像 .jav 代码在这里............

public class FirstImage extends ImageView {
    public FirstImage(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }




}

视频布局是代码如下....

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


  <!--  Screen Design for VIDEOS -->
<TableLayout 
    android:id="@+id/tablelayout"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:stretchColumns="1">

     <TableRow
          android:id="@+id/tableRow1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >

          <TextView
               android:id="@+id/textView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="15dip"
              android:text="pic on click which will tell where is the dna located in human body or cell "
              android:textSize="18dip" />
      </TableRow>

      <TableRow
          android:id="@+id/tableRow2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >

          <FrameLayout 
        android:layout_width="wrap_content"
          android:layout_height="wrap_content">
        <com.example.AndroidTablayout.FirstImage
            android:id="@+id/first"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:src="@drawable/human"/>                                               
</FrameLayout>
      </TableRow>

      <TableRow
          android:id="@+id/tableRow3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >

          <LinearLayout
              android:id="@+id/linearLayout1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

              <Button
                  android:id="@+id/button1"
                  android:layout_width="100dp"
                  android:layout_height="wrap_content"
                  android:text="Back" />

              <Button
                  android:id="@+id/button2"
                  android:layout_width="100dp"
                  android:layout_height="wrap_content"
                  android:text="Next" />

          </LinearLayout>

      </TableRow>
       <TableRow
          android:id="@+id/tableRow4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >
          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="15dip"
              android:text="please don't mind  "
              android:textSize="18dip" />
          </TableRow>

  </TableLayout>
  </ScrollView>
4

1 回答 1

1

编辑: 检查这个答案:

自定义图像视图android

也许您需要使用 AttributeSet 参数来实现构造函数。

于 2012-05-14T06:56:25.360 回答