2012-01-20 13 views
0

私はこのコードでNullpointer Exceptionを持っています。例外はmainLayout.setOnTouchListener(this)に表示されますが、私は理由を分からない。Nullpointer例外setOnTouchListener(this)

public boolean onTouch(View view, MotionEvent event) 
    { 
     switch (event.getAction()) 
     { 
     case MotionEvent.ACTION_DOWN: 
      fromPosition = event.getX(); 
      break; 
     case MotionEvent.ACTION_UP: 
      float toPosition = event.getX(); 
      if (fromPosition > toPosition) 
      { 
       flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.go_next_in)); 
       flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.go_next_out)); 
       flipper.showNext(); 
      } 
      else if (fromPosition < toPosition) 
      { 
       flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.go_prev_in)); 
       flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.go_prev_out)); 
       flipper.showPrevious(); 
      } 
     default: 
      break; 
     } 
     return true; 
    } 
} 

どうもありがとう:Aは、ここにいくつかのミスすることができる別の活動:

public class Assistantbuilder extends Activity implements OnTouchListener 
{ 
    private ViewFlipper flipper = null; 
    private float fromPosition; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.main); 

     LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout); 
     mainLayout.setOnTouchListener(this); 

     flipper = (ViewFlipper) findViewById(R.id.flipper); 

     LayoutInflater inflater = (LayoutInflater)   getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     int layouts[] = new int[]{ R.layout.dashboard_layout, R.layout.dashboard_layout2, R.layout.dashboard_layout3, R.layout.dashboard_layout4 }; 
     for (int layout : layouts) 
      flipper.addView(inflater.inflate(layout, null)); 

     Button btndimensions_rafters = (Button) findViewById(R.id.home_btn_dimensions_rafters); 
     btndimensions_rafters.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       switch (arg0.getId()) { 
       case R.id.home_btn_dimensions_rafters: 
        Intent i = new Intent(Assistantbuilder.this, 
          dimensions_rafters.class); 
        startActivity(i); 
        break; 
       } 
      } 
     }); 
     Button btngable_roof = (Button) findViewById(R.id.home_btn_gable_roof); 
     btngable_roof.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       switch (arg0.getId()) { 
       case R.id.home_btn_gable_roof: 
        Intent i = new Intent(Assistantbuilder.this, 
          GableRoof.class); 
        startActivity(i); 
        break; 
       } 
      } 
     }); 

    } 

にいくつかのコードを変更しました。

+0

ここにあなたの例外を追加することなく、どこに来るのか誰でも知ることができ、いくつかのログを追加すると思いますか? – AAnkit

答えて

1

あなたのアクティビティのonCreateにある行を考えてみると、thisは常に有効なはずですので、nullになる可能性のある唯一のものはmainLayoutです。 XMLレイアウトファイルを変更しましたか?実行時にmain_layoutLinearLayoutが見つかりません。

関連する問題