Simple code does not work - android

I have the following code:
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
public class MainActivity extends Activity {
AnimationDrawable mAnim;
ImageView mouse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mouse = (ImageView)findViewById(R.id.mouse);
mouse.setBackgroundResource(R.anim.eyes);
mAnim = (AnimationDrawable)mouse.getBackground();
Button kill = (Button)findViewById(R.id.kill);
kill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mAnim.start();
}
});
}
}
It gives an error despite it is very simple and unfortunately I cannot check logCat, because my comp is too weak to handle an emulator. Is there any idea what is wrong with this code?
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
tools:context=".MainActivity"
>
<ImageView
android:id="#+id/mouse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/desc"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/u1"
/>
<Button
android:id="#+id/kill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:background="#drawable/playselector" />
</RelativeLayout>
eyes.xml (animation file):
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/u1" android:duration="15"/>
<item android:drawable="#drawable/u2" android:duration="15"/>
<item android:drawable="#drawable/u3" android:duration="15"/>
....
<item android:drawable="#drawable/u28" android:duration="15"/>
</animation-list>

Your code is correct. I've tested it.
download :
http://ubuntuone.com/1hRi0O5UMYyPoqfQj76t2i

Thank you! I figured out what the problem was, my images were too big (2000x2830).

Related

my app is not running still in my phone but is not showing any error

It Shows no error on Android Studio but app even not starting on phone.Is there any problem with the code.I feel like there may be any mistakes in syntax but i am not able to find one.Please help me.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button bDriver,bCustomer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bDriver=(Button)findViewById(R.id.driver);
Button bCustomer=(Button)findViewById(R.id.customer);
bDriver.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new
Intent(MainActivity.this,DriverLoginActivity.class);
startActivity(intent);
finish();
}
});
bCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent ryt=new
Intent(MainActivity.this,CustomerLoginActivity.class);
startActivity(ryt);
finish();
}
});
}
}
And activity_main.xml is this and please explain if there is any problem related to SDK version also upload the detail.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.trillcore.uber.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/driver"
android:layout_below="#+id/customer"
android:text="I am a Driver"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a Customer"
android:layout_above="#+id/driver"
android:id="#+id/customer"/>
</RelativeLayout>

string aren't passing between my activities maybe xml issue in android studio

Please help me figure out why my string isn't passing between my activities in android studio. I ran this script on my phone but the string I'm passing from my main activity is passing to my secondary activity. I'm going through an android teach yourself book and I believe the issue is in my XML.
Here's my XML code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.owner.hour3application.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
activity_secondary.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.owner.hour2application.SecondaryActivity">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.design.widget.CoordinatorLayout>
Main Activity
package com.example.owner.hour2application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button activityButton = (Button) findViewById(R.id.Button);
if (activityButton != null) {
activityButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent startIntent = new Intent(MainActivity.this, SecondaryActivity.class);
startIntent.putExtra("com.example.owner.MESSAGE", "Hello SecondaryActivity");
startActivity(startIntent);
}
});
}
}
}
Secondary Activity
package com.example.owner.hour2application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class SecondaryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondary);
Intent intent = getIntent();
String message = intent.getStringExtra("com.example.owner.MESSAGE");
TextView messageTextView = (TextView) findViewById(R.id.message);
if (messageTextView != null) {
messageTextView.setText(message);
}
}
}
Thanks guys sorry I posted the wrong main activity. The issue was in string.xml the string name was wrong. Thanks!

Do not see any change on runtime eclipse android

I Change an activity background color in eclipse,but I don't any change in run time. why?
this is activity XML code,I use android:background="#ff0000" to change background cokor:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:content=".TestingDetailsActivity"
android:background="#ff0000">
<Button
android:id="#+id/btnExistsTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ExamList" />
</LinearLayout>
and java code:
package com.tutecentral.restfulapiclient;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class TestingDetailsActivity extends Activity {
Button btnExistsTest;
protected void onCreate() {
//super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing);
btnExistsTest=(Button) findViewById(R.id.btnExistsTest);
btnExistsTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(TestingDetailsActivity.this,ExamListActivity.class);
startActivity(i);
}
}) ;
}
}
Manually uninstall the APK file from your phone and then re-run the app from your IDE. Sometimes these kind of glitches happen.

Moving from start up screen to loading screen in android app

I set an android startup screen to my app, so that I can move directly after clicking on it to a loading screen (which is a frame animation). The problem is that my app closes directly when clicking on my startup screen. Can you please tell me what's wrong with my program?
Here is my main activity code:
package test.com.myapplication;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MyActivity extends Activity {
private AnimationDrawable frameAnimation;
private ImageView view;
ImageButton start;
RelativeLayout background;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
start = (ImageButton) findViewById(R.id.startup);
view = (ImageView) findViewById(R.id.imageAnimation);
frameAnimation = (AnimationDrawable) view.getBackground();
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
frameAnimation.start();
}
});
}
}
and the XML files i created :
layout file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/back"
tools:context=".MyActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/startup"
android:id="#+id/startup"
/>
<ImageView
android:id="#+id/imageAnimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
</RelativeLayout>
and the animation file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/frame01" android:duration="210"/>
<item android:drawable="#drawable/frame02" android:duration="210"/>
<item android:drawable="#drawable/frame03" android:duration="210"/>
<item android:drawable="#drawable/frame04" android:duration="210"/>
<item android:drawable="#drawable/frame05" android:duration="210"/>
<item android:drawable="#drawable/frame06" android:duration="210"/>
</animation-list>
If you want to run the animation inside the ImageView, you have to set background property of the ImageView with your animation file.
<ImageView
android:id="#+id/imageAnimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/animation" />

why am I getting errors when I use different backgrounds in xml

I have 3 activities which I want to use different backgrounds with. but when i do this i get this i get this error however if i only use different backgrounds on 2 activities then the app works
11-20 13:40:25.855: E/AndroidRuntime(849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.medepad.community_virtual_ward/com.medepad.community_virtual_ward.Temperature}: android.view.InflateException: Binary XML file line #2: Error inflating class
the activities are called main welcome and temperature.
code for main
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#drawable/main" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="400dp"
android:layout_marginTop="115dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="40dp" />
</RelativeLayout>
java code
package com.medepad.community_virtual_ward;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
int a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView start = (TextView)findViewById(R.id.textView1);
start.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent welcome= new Intent (this,Welcome.class);
startActivity(welcome);
}
}
code for temperature
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/temperature" >
</LinearLayout>
java code
package com.medepad.community_virtual_ward;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Temperature extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.temperature);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
welcome xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/welcome" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="400dp"
android:layout_marginTop="115dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="40dp" />
</RelativeLayout>
java code
package com.medepad.community_virtual_ward;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class Welcome extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
TextView next= (TextView)findViewById(R.id.textView2);
next.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent temperature= new Intent(this, Temperature.class);
startActivity(temperature);
}
}
why am i getting this error and what can i do to use the amount of backgrounds i want?
The end of your first XML file has an extra >.
Please check the following file
temperature.xml
Make sure, it has following contents
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/temperature" >
</LinearLayout>
and you have either defined a drawable resource with name temperature.xml and main.xml under res/drawable folder or you have added image resources with name temperature and main in your Android project.

Categories

Resources