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?
Related
I'm trying to make my application start another class.
What im trying to learn is how to get another class to run in the background - like if the user opens the application, the application stays running.
I thought if I could try to open another class by using an intent, it would work. When i run my application on the emulator, it just crashes...
Here is the opening:
package omg.justry;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
public void onCreate(Bundle savedInstanceState) {
Intent openStartingPoint = new Intent("omg.justtry.PartF**king2");
startActivity(openStartingPoint);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
Here is the "PartF**king2" class:
package omg.justry;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
public class PartF**king2 extends Activity{
public void onCreate(Bundle savedInstanceState) {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
The thing is, Eclipse doesn't show any errors. I just exported the app and installed it to the emulator using adb.
I also added the class to the AndroidManifest as you see here:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="omg.justry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="PartF**king2"></activity>
</application>
I think its the manifest now that I look at it but whatever i do, it gets an error or crashes with Eclipse not explaining anything.
In every class, you onCreate(Bundle savedInstanceState) method MUST contain
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
The super is absolutly mandatory, and the setContentView defines the layout for your activity.
And an Activity cannot "run in the background". Start by reading some Android tutorial, and you'll have some clues about what to do.
I'm beginner in Android (and java), and I'm just playing around and want to create new View after clicking a button. Here is my code so far:
Main class
package myTests.homeSpace;
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 TestsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener( new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(TestsActivity.this, screen1.class);
startActivity(myIntent);
}
});
}
}
Class to run after clicking button
package myTests.homeSpace;
import android.app.Activity;
import android.os.Bundle;
public class screen1 extends Activity {
#Override
public void onCreate(Bundle SavedBundleInstance)
{
super.onCreate(SavedBundleInstance);
setContentView(R.layout.screen1);
}
}
The problem is, after hitting button I got "Unforunately, Tests has stopped" error. ("Tests" is application name). I know (or guess) the problem is in this line Intent myIntent = new Intent(TestsActivity.this, screen1.class);
I guess my reference to class screen1 is wrong somehow, but I have no idea why. There are no compilation errors nor warnings, layout .xmls shouldn't be wrong.
Could any of you please advice me any solution?
EDIT
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myTests.homeSpace"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestsActivity"
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>
CONSOLE OUTPUT
[2012-06-20 19:56:21 - ddms] null
java.lang.NullPointerException
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:575)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:672)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
I bet if you read the crash log somewhere in there says did you forget to declare this activity in the Manifest?
I agree, not declaring the activity in your Manifest file (AndroidManifest.xml) is most likely your problem. See the android developer page on activities for more information. Here is the relevant description:
Declares an activity (an Activity subclass) that implements part of
the application's visual user interface. All activities must be
represented by elements in the manifest file. Any that are
not declared there will not be seen by the system and will never be
run.
I just posted this so you would have more space to research on your own.
Declare your second activity in the Manifest.xml as:
<activity
android:name=".screen1">
</activity>
Just below the declaration of your first activity.
I want to link my two activites by clicking on a button i have written the following code
public class IHBCAPTUREActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
ImageView iv;
TextView tx;
Button b1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.imageView1);
tx=(TextView)findViewById(R.id.textView1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent calIntent;
**calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);**
startActivity(calIntent);
}
}
error cumes in this line
calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);
for LoginActivity.class that
LoginActivity cannot be resolved to a type . How to solve it?
here is the simple code how you can load one activity to another,
i have created two activities like this
FirstActivity.java
package com.rdc;
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 FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button btnload = (Button) findViewById(R.id.btn);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
SecondActivity.java
package com.rdc;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
and my manifest file is
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity"
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=".SecondActivity"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
try to implement this in your app and let me know if still any doubt is there.
Check whether you have declared LoginActivity in manifest file as:
<activity
android:name="your.package-name.LoginActivity" >
</activity>
You don't have anything wrong in code.
Its know issue that occurs randomly.
solution is to set the target to other API level in project->properties->android, then set it back.then clean you project once.
I think this will refresh the .classpath or some other files, not sure, but it works.
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.
Code for QuizSplashActivity:
package com.androidbook.triviaquiz;
import java.text.DateFormat;
import java.util.Date;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class QuizSplashActivity extends QuizActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String rest="First Time Launched";
String launchd=DateFormat.getDateInstance().format(new Date());
String launcht=DateFormat.getTimeInstance().format(new Date());
String ldt=launchd+" "+launcht;
SharedPreferences set=getSharedPreferences(GAME_PREFERENCES,MODE_PRIVATE);
if(set.contains("lastLaunch"))
{rest=set.getString("lastLaunch", "default");
}
Log.i("LaunchInfo",rest);
SharedPreferences.Editor edit=set.edit();
edit.putString("lastLaunch",ldt);
edit.commit();
setContentView(R.layout.splash);
anime();
}
private void anime()
{TextView t1=(TextView)findViewById(R.id.TextViewTopTitle);
Animation f1=AnimationUtils.loadAnimation(this, R.anim.fade_in);
t1.startAnimation(f1);
TextView t2=(TextView)findViewById(R.id.TextViewBotTitle);
Animation f2=AnimationUtils.loadAnimation(this, R.anim.fade_in2);
//t2.startAnimation(f2);
// Animation fade2 =AnimationUtils.loadAnimation(this, R.anim.fade_in2);
//View.startAnimation(fade2);
t2.startAnimation(f2);
AnimationListener animListener=new AnimationListener() {
public void onAnimationEnd(Animation animation){
startActivity(new Intent(QuizSplashActivity.this,QuizMenuActivity.class));
QuizSplashActivity.this.finish();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
};
f2.setAnimationListener(animListener);
Animation spinin=AnimationUtils.loadAnimation(this,R.anim.custom_anim);
LayoutAnimationController controller=new LayoutAnimationController(spinin);
TableLayout tb=(TableLayout) findViewById(R.id.tableLayout1);
for(int i=0;i<tb.getChildCount();i++)
{TableRow row=(TableRow) tb.getChildAt(i);
row.setLayoutAnimation(controller);
}
}
#Override
protected void onPause()
{super.onPause();
TextView t1=(TextView)findViewById(R.id.TextViewTopTitle);
t1.clearAnimation();
TextView t2 =(TextView)findViewById(R.id.TextViewBotTitle);
t2.clearAnimation();
TableLayout tb=(TableLayout) findViewById(R.id.tableLayout1);
for(int i=0;i<tb.getChildCount();i++)
{TableRow row=(TableRow) tb.getChildAt(i);
row.clearAnimation();
}
}
#Override
protected void onResume()
{super.onResume();
anime();
}
}
Code for QuizMenuActivity
package com.androidbook.triviaquiz;
import android.os.Bundle;
public class QuizMenuActivity extends QuizActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
}
}
After the animations in splash have played out (Specifically fade_in2) the QuizMenuActivity is supposed to be launched and splash finished. The Animations play out fine but the QuizMenuActivity doesn't seem to be launched. Playing around with the debugger/break points it seems the program flow never goes into onAnimationEnd (Animation animation) method. Though I might be wrong as I am pretty new with eclipse and android.
I've fixed the code based on the first reply and updated it.Now the listner is firing but after the start activity i get 'application has beenstopped unexpectedly.Please try again.Force Close'.Seems to be while in something called ZygoteInit$MethodAnd.
Is this the callstack:
tiv [Android Application]
DalvikVM[localhost:8612]
Thread [<3> main] (Running)
Thread [<13> Binder Thread #2] (Running)
Thread [<11> Binder Thread #1] (Running)
I think the callstacks supposed to be in the logcat but I'm getting 'Unable to open stack trace file '/data/anr/traces.txt': Permission denied' error log in logcat.
Also the appliction seems to crash before the the animation f2(which is what is being listened for) has been displayed.t2 never gets displayed.
-change manifest to this
-It solved my problem
-add dot in the beginning of activity name, not sure why or if it is necessary !
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidbook.triviaquiz7"
android:versionCode="1"
android:versionName="1.0">
<application
android:label="#string/app_name"
android:debuggable="true"
android:icon="#drawable/quizicon">
<activity
android:name=".QuizSplashActivity"
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=".QuizGameActivity"></activity>
<activity
android:name=".QuizSettingsActivity"></activity>
<activity
android:name=".QuizScoresActivity"></activity>
<activity
android:name=".QuizHelpActivity"></activity>
<activity
android:name=".QuizMenuActivity"></activity>
</application>
<uses-sdk
android:minSdkVersion="7" />
It seems you are adding the animation listener to the 'fade2' animation object. But that animation is not set to any view like
view.startAnimation(fade2);
Without starting the animation for a view the listener will not fire.