1

我正在创建一个抛出错误的应用程序,活动未启动。我的活动课是:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videos_layout);
    i = new FirstImage(this);
    c = (ViewFlipper) findViewById(R.id.viewFlipper3);
    back = (Button) findViewById(R.id.button1);
    next = (Button) findViewById(R.id.button2);
    t = (TextView) findViewById(R.id.textView1);
    if(count == 0)
        i.changeImage(R.drawable.human); 
    c.addView(i);
    c.setOnTouchListener(this);
    c.onTouchEvent(event);
    addListenerOnButton();
    }


    private void addListenerOnButton() {
        if (count == 0)
            back.setEnabled(false);
        next.setOnClickListener( new OnClickListener() {

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

                addListenerOnButton();

                }
                else if (count==1){
                    i.changeImage(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);

                    }
                }
            });
    }




public void pageinfo(float a,float b){

        t.setText(Float.toString(a)+"x"+Float.toString(b)+"y");

    }



public boolean onTouch(View v, MotionEvent me) {
    // TODO Auto-generated method stub
    switch(me.getAction()){
    case MotionEvent.ACTION_DOWN:
        a=me.getX();
        b= me.getY();
        pageinfo(a,b);
        break;
    case MotionEvent.ACTION_MOVE:
        a=me.getX();
        b= me.getY();
        pageinfo(a,b);
        break;
    case MotionEvent.ACTION_UP:
        a=me.getX();
        b= me.getY();
        pageinfo(a,b);
        break;
    case MotionEvent.ACTION_OUTSIDE:
        a=me.getX();
        b= me.getY();
        pageinfo(a,b);
        break;
    default: return false;
    }
    return true;

}
}

我的FirstImage班级代码:

public class FirstImage extends ImageView {

    float a=0; 
    float b=0;
    TextView t;
    ImageView i;

    public FirstImage(Context context) { 
        super(context); 
        t= (TextView) findViewById(R.id.textView1); 
        i= new ImageView (context); 
    } 

    public FirstImage(Context context, AttributeSet attrs) { 
        super(context, attrs);
   } 

    protected void changeImage(int id){ 
        i.setImageResource(id); 
    } 

堆栈跟踪异常:

  05-11 00:21:41.197: E/AndroidRuntime(1142): FATAL EXCEPTION: main
05-11 00:21:41.197: E/AndroidRuntime(1142): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtablayout/com.example.androidtablayout.VideosActivity}: java.lang.NullPointerException
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.view.View.performClick(View.java:2408)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.view.View$PerformClick.run(View.java:8816)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.os.Handler.handleCallback(Handler.java:587)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.os.Looper.loop(Looper.java:123)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at java.lang.reflect.Method.invoke(Method.java:521)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at dalvik.system.NativeStart.main(Native Method)
05-11 00:21:41.197: E/AndroidRuntime(1142): Caused by: java.lang.NullPointerException
05-11 00:21:41.197: E/AndroidRuntime(1142):     at com.example.androidtablayout.VideosActivity.onCreate(VideosActivity.java:45)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 00:21:41.197: E/AndroidRuntime(1142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-11 00:21:41.197: E/AndroidRuntime(1142):     ... 18 more
4

3 回答 3

1

它在 FirstImage 类第 22 行给出 nullpointer 异常。只是因为未启动活动。首先解决 nullpointer 异常

于 2012-05-15T05:45:43.737 回答
1

您应该始终在方法中初始化视图onCreate

//...
FirstImage i;
//...

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             setContentView(R.layout.videos_layout);
             i = new FirstImage(this);
    //...

同样在FirstImage类中初始化Views构造函数中的那些:

public FirstImage(Context context) {
    super(context);
   TextView t= (TextView) findViewById(R.id.textView1); // WHERE do you expect to find this view, in which layout?
   ImageView i= new ImageView (context); //NEVER pass a null Context
}

编辑:

我的建议是遵循一些自定义教程Views。你不能将其他添加ViewsImageView子类中(你不应该)。即使您ImageViewFirstImagechangeImage方法中构建,也会抛出NullPointerException您的代码当前构建的方式。

一个简单的自定义视图:

 public class FirstImage extends LinearLayout {
    float a=0; 
    float b=0;

    TextView t;
    ImageView i;

    public FirstImage(Context context) { 
        super(context); 
        t = (TextView) findViewById(R.id.textView1); // What is with this TextView?!?
        i = new ImageView (context); 
        LinearLayout.LayoutParams lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        addView(i, lp);   
    } 

protected void changeImage(int id){ 
    i.setImageResource(id); 
} 
于 2012-05-15T05:49:48.427 回答
1

首先你应该像这样膨胀你的布局:

TextView t;
ImageView i;
LayoutInflater inflater; 
public FirstImage(Context context) {
super(context);
inflater = LayoutInflater.from(context);
View v=inflater.inflate(R.layout.yourlayout, null);
t= (TextView)v.findViewById(R.id.textView1);
i= new ImageView (context);


 }
于 2012-05-15T06:03:29.247 回答