Unable to Override OnCreate of an Activity [closed] - android

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have 2 Activities, one which is MainActivity and the other, ReadActivity.
Whenever I started ReadActivity from MainActivity, the Activity started but it shows nothing, just the android:label value in the Manifest file.
As I was investigating, I tried putting a Log in ReadActivity but I noticed that the OnCreate method isn't called as I forgot to override the method but when I put the "#Override" above the OnCreate method I get the followong error:
"The method OnCreate(Bundle) of type ReadActivity must override or implement a supertype method".
I don't get what's wrong with this line, I've crossed checked it so many times, compared the codes with other application's code but I just can't seem to override it. Can anyone help me? Below are the codes of my project.
MainActivity
package com.rangga.elearning.ngaji;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity{
/** Called when the activity is first created. */
TextView txtLogo, txtVersion;
Button btnRead, btnIndex, btnAbout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
//Typeface font = Typeface.createFromAsset(getAssets(), "assets/Schoolbell.ttf");
txtLogo = (TextView) findViewById(R.id.txtLogo);
txtVersion = (TextView) findViewById(R.id.txtVersion);
btnRead = (Button) findViewById(R.id.btnRead);
btnIndex = (Button) findViewById(R.id.btnIndex);
btnAbout = (Button) findViewById(R.id.btnAbout);
/* txtLogo.setTypeface(font);
txtVersion.setTypeface(font);
btnRead.setTypeface(font);
btnIndex.setTypeface(font);
btnAbout.setTypeface(font); */
btnRead.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
callIntent();
}
});
}
public void callIntent(){
Intent i = new Intent(this, ReadActivity.class);
startActivity(i);
}
}
ReadActivity
package com.rangga.elearning.ngaji;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class ReadActivity extends Activity{
private static String TAG = "ReadActivity";
#Override
public void OnCreate(Bundle savedInstanceState){
Log.i(TAG, "ReadActivity dimulai");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.read);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rangga.elearning.ngaji"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="7"
android:maxSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ReadActivity"
android:label="Baca"
android:screenOrientation="landscape">
</activity>
</application>
</manifest>
Thank you.

onCreate() not OnCreate() . Besides, your manifest doesn't contain Intent information for your ReadActivity.

Related

Android Step 1 from Root Developer Page Activity not Working

I attempted to run "Clean Code" according to this link, however nothing is happening. From being a Renegade developer my intuition says "Button onClick" is missing but I could be wrong. Here is my code written according to what the instructions are:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class BuildingaSimpleUserInterfaceApi8Activity extends Activity {
public final static String EXTRA_MESSAGE = "com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/**This is how I work with Buttons and the Next Activity, however this method IS NOT defind
* in the very first Android Tutorial for sending information onClick.
Button bButtonone = (Button) findViewById(R.id.button_send);
bButtonone.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY"));
}
});
}
*/
/** Called when the user selects the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.developer.android.com_training_basics_firstapp_building_ui"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BuildingaSimpleUserInterfaceApi8Activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.developer.android.com_training_basics_firstapp_building_ui.DisplayMessageActivity" />
</application>
</manifest>
It looks like you forgot the line to start the activity:
public void sendMessage(View view) {
...
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent); // add me!
}
I read through the code at the site listed and you have missed one line:
startActivity(intent);
at the end od sendMessage.

Using intents to switch activities

I am reading a android book for beginners, and while following on of the chapters I ran into a problem. The chapter is teaching about intents. I right now I have 2 layouts: main.xml, and digital_clock.xml. And in the AndroidManifest I have these lines of code:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".Chapter11Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DigitalClockActivity" >
</activity>
</application>
Also I have the two Activity classes that correspond to the layouts:
Chapter11Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Chapter11Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button a2 = (Button)findViewById(R.id.button1);
a2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), DigitalClockActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
}
DigitalClockActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class DigitalClockActivity extends Activity {
public void OnCreate(Bundle sIS) {
super.onCreate(sIS);
setContentView(R.layout.digital_clock);
Button a1 = (Button) findViewById(R.id.button01);
a1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent replyIntent = new Intent();
setResult(RESULT_OK, replyIntent);
finish();
}
});
}
}
When I run the app on my phone and switch to the second activity it shows nothing at all. Am I defining something wrong in the AndroidManifest? The application seems very straight forward, but it is not working. I have check over to make sure I didn't type anything wrong. Is it because I am running a android 2.3.3 phone and using the 1.5 sdk, and something is not backwards compatible? Any answers are appreciated!
~Andrew
Method name in second activity should be onCreate not OnCreate.
To prevent typos like this in the future use #Override on methods that you are overriding:
#Override
public void OnCreate(Bundle sIS) {
// code here
}
Then if you make a typo, the compiler will tell about it.

getting exception when starting new activity on an android

i have read a lot about this but didnt come up with a solution.
this is my first android project.
i am trying yo open an activity from my main activity.
i have doubled check the names and the configuration files, but the startactivity() procedure fails.
debbuger shows runtime errror.
as u will c, the startactivity is called upon onclick event.
i am really stuck here dont know what else to do.
thanks a lot!
* this is the main activity **
package com.example.helloandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
private OnClickListener gtScoringListener = new OnClickListener() {
public void onClick(View v) {
Intent mintent = new Intent(HelloworldActivity.this ,imgScoring.class);
startActivity(mintent);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(gtScoringListener);
}
}
* this is the target activity: *
package com.example.helloandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class imgScoring extends Activity {
private OnClickListener backBtnListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(imgScoring.this, HelloworldActivity.class);
startActivity(intent);
}
};
private OnClickListener scoreBtnListener = new OnClickListener() {
public void onClick(View v){
TextView tv = (TextView)findViewById(R.id.editText1);
tv.setText(((Button)v).getText());
}
};
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button01);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button03);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button02);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button04);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.button3);
button.setOnClickListener(backBtnListener);
setContentView(R.layout.imgscoring);
}
}
this is the androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="12" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".imgScoring"></activity>
<activity android:name=".HelloworldActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You should've posted the stacktrace, but I assume the error is on this line in your target activity:
button.setOnClickListener(scoreBtnListener);
This will give you a NULL:
Button button = (Button)findViewById(R.id.button1);
because you haven't set a contentview in THAT activity, so there is nothing for findViewById() to return. You'll get a nullpointer when calling that setOnclickLIstener()
Try change class name from imgScoring to ImgScoring everywhere. Java is not allowing class name to start from small letter ;-)
And place setContentView(R.layout.imgscoring); just after your super.onCreate();

Android execution error in AVD

an anyone look at this simple code (?) and tell me what's wrong please?
I'm a complete beginner to android development and I don't understand why my application doesn't even start. I get an unexpected error.. : (
Here it is:
package applicationTest.ppr.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainClass extends Activity {
/** Called when the activity is first created. */
/*Global vars*/
public static LinearLayout lila;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lila = (LinearLayout) findViewById(R.id.lilay);
setContentView(lila);
}
public void Shortoast(){new Game(this);}
public static LinearLayout returnLayout(){return lila;}
}
The program doesn't even launch, and I think it might have something to do with how I handle the LinearLayout and setContentView();
anyway thanks very much in advance!
Suggestion: keep it as simple as possible until you fix the issue!
Then you can concentrate on your business logic.
package applicationTest.ppr.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainClass extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.lilay);
}
}
Also, is your main activity mapped in the Android manifest?
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="MainClass"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</activity>
--EDIT--
Do you have the lilay.xml file in your res/layout folder?

JAVA - Android error: The application has stopped unexpectedly please try again

I made an app which is just working fine. But when i click on "cancel" or "ok" button when i run the program, instead of linking me to result.java, the simulator give me "The application has stopped unexpectedly please try again" Here's the code
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.database.CursorJoiner.Result;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
public class lib_main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onAddClick (View botton){
AutoCompleteTextView title = (AutoCompleteTextView)findViewById(R.id.Title);
AutoCompleteTextView author = (AutoCompleteTextView)findViewById(R.id.Author);
AutoCompleteTextView isbn = (AutoCompleteTextView)findViewById(R.id.ISBN);
Intent intent = new Intent();
intent.setClass(this,Result.class);
intent.putExtra("Title", title.getText().toString());
intent.putExtra("Author", author.getText().toString());
intent.putExtra("ISBN", isbn.getText().toString());
startActivity(intent);
}
public void onCancelClick(View botton){
Intent intent = new Intent();
intent.setComponent(new ComponentName(this,Result.class));
intent.putExtra("Cancel","Cancel");
startActivity(intent);
}
}
this is Result.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Result extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView resultText=(TextView)findViewById(R.id.resultText);
Bundle bundle =getIntent().getExtras();
if(bundle.getString("Cancel")!= null)
{resultText.setText("operation cancel");}
else
{ String book =bundle.getString ("Title");
String title =bundle.getString ("Author");
String isbn =bundle.getString ("ISBN");
resultText.setText(
getString (R.string.resultOk)+""+book+""+ title+""+isbn);
}
}
}
the error log write "No command output when running:'am start-n com.Library.doc/com.Library.doc.lib_main-a android.intent.action.MAIN-c android.intent.category.LAUNCHER' in device emulator-5554"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Library.doc"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".lib_main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Result"
android:label="#string/result">
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
Can anyone help me with this problem?
Quick question, have you registered both Activities? If you only provide for one in your manifest file you're going to get this error every time you attempt to do something involving the unregistered activity.

Categories

Resources