hello,
I am learning to develop on android and I have an issue. I hope this is the right place to find help. I tried the development forum but apparently I can't post there.
So here is my issue:
I'm trying to create a header for my application. In this header, I have a button, and since I don't want to duplicate my button click handler in every activity, I created a MotherActivity (that inherits Activity) from which my activities will inherit from.
In my first activity, I wrote:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myActivity1); (myActivity1 <includes> layout.header)
}
and in my MotherActivity, I used:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout include_header = (LinearLayout)(findViewById(R.layout.header));
((ImageView)( include_header.findViewById(R.id.ImageButton1)))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
});
}
My problem is that findViewById(R.layout.header) returns null.
If I try to invert onCreate() and setContentView(), I receive this error:
ERROR/AndroidRuntime(872): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
So I don't know what to do, in both cases I receive an error...
Can you help me please ?
Thanks.
I managed to fix the error.
But there's something I don't understand. The whole point of this architecture with an inherited activity was to create a button in the header, and whose event handler wouldn't need to be rewritten in all the activities.
But the button doesn't do anything when clicked on.
How is that possible ?
Related
Hi guys,
I am writing a program which has to display content provided by a web platform. Therefore, I split the program up into four classes. The "Main" class (Activity) just initializes the Controller "AppScheduler" (Intent) which then initializes the "PlatformConnector" (Service) and the "UI" (Activity).
Since I am a newbie in programming apps for Adroid, I am having trouble with getting setup a view in the UI. In case I define a view there, the app returns an exception. Does anyone of you knows what I am doing wrong?
Main Class:
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppScheduler controller = new AppScheduler(this);
controller.Initialize();
// everything else happens in AppScheduler class
}
}
-------------------------
AppScheduler Class:
public class AppScheduler extends Intent {
// PLATFORM CONNECTOR ATTRIBUTES
protected PlatformConnector pfconnect = null;
// VIEW ATTRIBUTES
protected UI uiMain = null;
// AppScheduler Constructor
public AppScheduler(Main main) {
// TODO Auto-generated constructor stub
}
public void Initialize (){
//initialize Web Service Connector
pfconnect = new PlatformConnector();
pfconnect.initialize();
// create and initalize GUI
uiMain = new UI();
uiMain.onCreate(null);
}
}
---------------------------
UI Class:
public class UI extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
The Error occurs in case I include "setContentView(R.layout.main);". Note: The main.xml exists and normally runs without any problems.
I don't know why you are using the Intent the way you are. It isn't intended to be used as a controller class base.
An intent is meant invoke an event in the system, and the system figures out the best way to handle it.
Oh and you are trying to instantiate an Activity class and show it; you aren't supposed to do that either. My guess is you haven't done any Android development yet. I recommend starting with the basics and the sample programs.
http://code.google.com/android/intro/hello-android.html
My question is relatively simple; is the code below an accepted method of changing full screen menus in Android 2.1? I thought I should ask, because when I switch back using the back key without doing the findViewById again, the menu doesn't work. So I have to reconstruct the menu each time (createMainMenu() method). To implement the back key I use a state, which is dependant upon which menu the system is in.
Also, I thought using multiple activities was not necessary in this small application.
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createMainMenu();
}
private void createMainMenu(){
setContentView(R.layout.main);
state=STATE_MAIN;
createKeyButton=(Button)findViewById(R.id.createKeyButton);
encryptTextButton=(Button)findViewById(R.id.encryptTextButton);
decryptTextButton=(Button)findViewById(R.id.decryptTextButton);
createKeyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
createKeyCreationMenu();
}
});
}
private void createKeyCreationMenu(){
setContentView(R.layout.create_key);
state=STATE_MENU;
keyLengthField=(EditText)findViewById(R.id.keySizeEditText);
}
public void onBackPressed(){
if(state==STATE_MENU){
createMainMenu();
} else if(state==STATE_MAIN){
super.onBackPressed();
}
return;
}
SOLVED
Although this method works, you get problems when the user turns the screen. I have resolved to launching new activities for new full screen menu's.
Can a moderator please move this thread to the android software forum?
Hi, I'm new to android programming. i just started with hello world then change the textview from java. When I use the setext, app stopped working. Anyone pls help
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView mTextView = (TextView) findViewById(R.id.textView1);
mTextView.setText("Some Text");
setContentView(R.layout.activity_main);
}
I don't know what's wrong in that. Just today i downloaded the adt bundle and API 18.
you gotta set your contentview first.
read this thread for future problems.
http://forum.xda-developers.com/showthread.php?t=2325164
So i have created a pop up window with the source code below. I also have designed the popup really good. and it displays right. The only problem i have is the damn button to close it. lol I had it so it closed fine before. Then i started adding stuff in to make the app work the way i want it to.
So What i have done is this, I have the popup created i want to be able to have the Text box checked for a string if this string exists then I need to go to thread allApps which then launches my 2nd Activity which that works fine as well. But the problem is the damn pop uup is still up so i need to close this. Any way of doing this with using the code I have or do i need to redo something. Please Any help thanks
PHP:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final Button btnOpenPopup = (Button)findViewById(R.id.apps_button);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(btnOpenPopup, Gravity.CENTER, 0, 0);
}});
}
public void allApps(View arg)
{
Intent intent = new Intent(arg.getContext(), AppList.class);
startActivity(intent);
}
Hi all
I have programmed my first app, a calculator using webview, i have one main question
When rotating the webview it did clear itself, i have solved this using the following code
protected void onSaveInstanceState(Bundle outState) {
Screen.saveState(outState);
outState.putString("editTextData1", edt1.getText().toString());
super.onSaveInstanceState(outState);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
{
((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);
}
Click to expand...
Click to collapse
However the cursor for my calculator moves back to the start of the webview upon rotation and hence when i type it deletes the existing sum from before the rotation, please help