如何参加特定活动?

时间:2010-09-28 11:35:17

标签: java android android-activity android-intent

我的申请是如何构建的......

Class A extends Activity {
    Button cButton = (Button) findViewById(...);
    Button dButton = (Button) findViewById(...);

    //invoked when cButton is clicked
    methodButtonC() { 
        //Here I want to go to the activity of class C and this is how I have tried to solve it...  
        Intent intent = new Intent(A.this, C.class);
        startActivity(intent);
    }
    //invoked when dButton is clicked
    methodButtonD() { 
         //Same problem as above but with Class D
    }
} 

// Base Class for C & D, No real purpose except sharing som methods to C & D (should probobly be abstract?)
Class B extends A {  
    // vairables and methods for inheritance
} 

Class C extends B {
    // uses derived methods and variables from Class B
}

Class D extends B {
    // uses derived methods and variables from Class B
}

当我运行应用程序并单击按钮C或D时发生错误。我该怎么做?

1 个答案:

答案 0 :(得分:0)

在您尝试使用findViewById(...)

查找按钮之前,我认为您必须覆盖onCreate并通过调用setContentView(...)从xml加载视图
@Override public void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.<yourview>);

  buttonX = findViewById (...);
}