开发者

Inheriting methods with contexts in an activity

开发者 https://www.devze.com 2023-04-06 20:27 出处:网络
I have a base class which has a bunch of buttons that are going to be used on all my activities as a general navigation bar.

I have a base class which has a bunch of buttons that are going to be used on all my activities as a general navigation bar.

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);     
 开发者_JAVA技巧           GenericActivity.this.startActivity(IndexActivity);
        }
    });       
}
}

I then inherit this activity in another activity

public class Index extends GenericActivity {

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

The XML file of the index layout have buttons with the same IDs as the base class. When I start the Index activity my emulator crashes. Help!

EDIT: Stacktrace, but I cant seem to get the whole trace. Logcat shows "... 11 more" at the end http://pastebin.com/v64RyG2S


Try this:

remove setContentView(R.layout.main_menu); from GenericActivity.

What main_menu.xml has?

and change these methods calls order on Index class:

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


Ok, I suppose you want to show the same menu (main_menu.xml) on all your view. Correct?.

Your all calling setContentView 2 times on the same activity... so in the best case the second layout overwrite the first.

So, I have an idea: You could Override the method setContentView of GenericActivity and write something like that:

Firstly, you have to reserve a space for Index activity View in your main_menu layout, for example:

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

Then you have to delete your onCreate or do that:

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

And finally:

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);
} 

It's an idea, I hope it's help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消