【问题标题】:How to go to a specific activity?如何参加特定活动?
【发布时间】:2010-09-28 11:35:16
【问题描述】:

我的应用程序的结构...

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 时发生错误。我该怎么做?

【问题讨论】:

  • AndroidManifest.xml中提到你的所有活动吗?
  • 发生什么错误?至少给我们那么多。很有可能它说明了你做错了什么。
  • 给你的按钮和你的类同名看起来让我有点困惑......
  • 1.是的,每个活动都在 AndroidManifest.xml 2. 错误是 nullPointerException

标签: java android android-activity android-intent


【解决方案1】:

我认为在尝试使用 findViewById(...) 查找按钮之前,您必须通过调用 setContentView(...) 覆盖 onCreate 并从 xml 加载视图

@Override public void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.<yourview>);

  buttonX = findViewById (...);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    相关资源
    最近更新 更多