在活动中使用上下文继承方法

时间:2011-09-23 17:06:41

标签: android inheritance android-activity

我有一个基类,它有一堆按钮,将作为常规导航栏用于我的所有活动。

public abstract class GenericActivity extends Activity {

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

    final Button buttonFinder = (Button) findViewById(R.id.buttonNavigationFinder);
    buttonFinder.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent IndexActivity = new Intent(GenericActivity.this, Finder.class);     
            GenericActivity.this.startActivity(IndexActivity);
        }
    });

    final Button buttonIndex = (Button) findViewById(R.id.buttonNavigationIndex);
    buttonIndex.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent IndexActivity = new Intent(GenericActivity.this, Index.class);     
            GenericActivity.this.startActivity(IndexActivity);
        }
    });

    final Button buttonStart = (Button) findViewById(R.id.buttonNavigationStart);
    buttonStart.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent IndexActivity = new Intent(GenericActivity.this, MainMenu.class);     
            GenericActivity.this.startActivity(IndexActivity);
        }
    });       
}
}

然后我在另一个活动中继承了这个活动

public class Index extends GenericActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.index);
}
}

索引布局的XML文件具有与基类具有相同ID的按钮。当我启动Index活动时,我的模拟器崩溃了。救命啊!

编辑:Stacktrace,但我似乎无法得到完整的痕迹。 Logcat最后显示“...... 11以上” http://pastebin.com/v64RyG2S

2 个答案:

答案 0 :(得分:2)

试试这个:

setContentView(R.layout.main_menu);删除GenericActivity

main_menu.xml有什么?

并在Index class:

上更改这些方法调用顺序
@Override
public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.index);
    super.onCreate(savedInstanceState);
}

答案 1 :(得分:0)

好的,我想您要在所有视图上显示相同的菜单(main_menu.xml)。正确的吗?。

你在同一个活动上调用setContentView 2次...所以在最好的情况下,第二个布局会覆盖第一个布局。

所以,我有一个想法:你可以覆盖GenericActivity的方法setContentView并写下这样的东西:

首先,您必须在main_menu布局中为索引活动视图预留空间,例如:

....
<LinearLayout android:id="@+id/childActivity" ...></LinearLayout>
<Button android:id="@+id/buttonNavigationFinder" ...></Button>
<Button android:id="@+id/buttonNavigationIndex" ...></Button>
<Button android:id="@+id/buttonNavigationStart" ...></Button>
....

然后你必须删除你的onCreate或者那样做:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

最后:

public void setContentView(int resource){

    super.setContentView(R.layout.main_menu); // ---> Attention it's main_menu <---

    final Button buttonFinder = (Button) findViewById(R.id.buttonNavigationFinder);
    buttonFinder.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent IndexActivity = new Intent(GenericActivity.this, Finder.class);     
            GenericActivity.this.startActivity(IndexActivity);
        }
    });

    final Button buttonIndex = (Button) findViewById(R.id.buttonNavigationIndex);
    buttonIndex.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent IndexActivity = new Intent(GenericActivity.this, Index.class);     
            GenericActivity.this.startActivity(IndexActivity);
        }
    });

    final Button buttonStart = (Button) findViewById(R.id.buttonNavigationStart);
    buttonStart.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent IndexActivity = new Intent(GenericActivity.this, MainMenu.class);     
            GenericActivity.this.startActivity(IndexActivity);
        }
    }); 


    //And now you have to fill the space for the child Activity with Index (or others) Activity view
    LinearLayout childActivity = (LinearLayout) generalView.findViewById(R.id.childActivity);
    View activityView = View.inflate(this,resource,null); //--> Now it's resource <--
    childActivity.add(activityView);
} 

这是一个想法,我希望它对你有所帮助。