I am trying to create 8 buttons in one activity that will open their own activity. What am I missing? I am using Android Studio 1.1.0.
I get this error
java.lang.IllegalStateException:
Could not find a method AppleActivity(View) in the activity class
com.hashmi.omar.vodafonemobilephoneshop.Picker for onClick handler
on view class android.widget.Button with id 'button2'
Below is the Picker.class:
package com.hashmi.omar.vodafonemobilephoneshop;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.hashmi.omar.vodafonemobilephoneshop.util.SamsungActivity;
public class Picker extends Activity implements View.OnClickListener {
Button button2, button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker);
//Sets the font to Vodafone Light
Typeface vodaLt = Typeface.createFromAsset(getAssets(), "VODAFONELT.TTF");
TextView vodaHeading = (TextView)findViewById(R.id.textView4);
vodaHeading.setTypeface(vodaLt);
//Sets up a button
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
private void buttonapple(){
startActivity(new Intent(Picker.this, AppleActivity.class));
}
private void buttonsam(){
startActivity(new Intent(Picker.this, SamsungActivity.class));
}
public void onClick(View v) {
switch (v.getId())
{
case R.id.button2:
buttonapple();
break;
case R.id.button3:
buttonsam();
break;
}
}
}
Below is the activity_picker.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.hashmi.omar.vodafonemobilephoneshop.Picker"
android:background="#ffffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please choose a brand"
android:id="#+id/textView4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ffff0000"
android:textSize="31dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/appsel"
android:id="#+id/button2"
android:layout_column="0"
android:onClick="#string/title_activity_apple" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/samselect"
android:id="#+id/button3"
android:layout_column="20"
android:onClick="#string/title_activity_samsung" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/sonyselect"
android:id="#+id/button4"
android:layout_column="0" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/htcselect"
android:id="#+id/button5"
android:layout_column="20" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/bbselect"
android:id="#+id/button6"
android:layout_column="0" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/nokiaselect"
android:id="#+id/button7"
android:layout_column="20" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/nexselect"
android:id="#+id/button8"
android:layout_column="0" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/lgselect"
android:id="#+id/button9"
android:layout_column="20" />
</TableRow>
</TableLayout>
Here is AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".HomeScreen"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Picker"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".AppleActivity"
android:label="#string/title_activity_apple"
android:parentActivityName=".Picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
<action android:name="com.hashmi.omar.vodafonemobilephoneshop.AppleActivity" />
</activity>
<activity
android:name=".SamsungActivity"
android:label="#string/title_activity_samsung"
android:parentActivityName=".Picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
</activity>
<activity
android:name=".util.SamsungActivity"
android:label="#string/title_activity_samsung"
android:parentActivityName=".Picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
</activity>
</application>
Remove the simply onClick form your button2 because you are implemententing View.OnClickListener.
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/appsel"
android:id="#+id/button2"
android:layout_column="0"
/>
You have written this:
<Button
... other stuff here ...
android:onClick="#string/title_activity_apple" />
If you use #string/title_activity_apple as the value of an attribute, Android will look up the string, and act as if that string's value was the actual value. So if the string has the value "AppleActivity", then it's equivalent to writing android:onClick="AppleActivity".
Next: what does setting android:onClick do? It tells the button the name of a method to call on the current activity when the button is clicked - see here. So you have told the button to call the method AppleActivity in the current activity when the button is pressed. (As you can also see on that page, it also expects to call a method with one parameter of type View - the arguments matter too)
Your activity class (the one containing the button0 does not have a method called AppleActivity which takes a View parameter. So the button can't find the method you told it to call, so it throws an exception (which crashes the app). This is exactly what the exception message said - the button could not find a method AppleActivity(View) in the activity class com.hashmi.omar.vodafonemobilephoneshop.Picker, which it was looking for because of your onClick attribute, on a android.widget.Button with id 'button2'.
You need to create Method named in your xml file in your activity class as well. instead of creating following method
private void buttonapple(){
startActivity(new Intent(Picker.this, AppleActivity.class));
}
private void title_activity_apple(){
startActivity(new Intent(Picker.this, AppleActivity.class));
}
After above change check again.
Related
So Ive made my first animation screen for my phone, this works perfect, and now I wanted to try it out on my tablet, but it crashes.
The tablet is a Lenovo TAB3 7 Essential / Samsung SM-T550.
As you can see in the picture above it's line 34 setContentView(R.layout.activity_startscreen); the error occurs.
I've tried to Clean my project, but i think it might be in the .xml file?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#73FFFFFF"
android:orientation="vertical"
tools:context=".activitys.Startscreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="#+id/first_line"
android:layout_width="20dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:background="#drawable/blue_line" />
<View
android:id="#+id/second_line"
android:layout_width="20dp"
android:layout_height="200dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/first_line"
android:background="#drawable/gray_line" />
<View
android:id="#+id/third_line"
android:layout_width="20dp"
android:layout_height="150dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/second_line"
android:background="#drawable/blue_line" />
<View
android:id="#+id/fourth_line"
android:layout_width="20dp"
android:layout_height="250dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/third_line"
android:background="#drawable/gray_line" />
<View
android:id="#+id/fifth_line"
android:layout_width="20dp"
android:layout_height="100dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/fourth_line"
android:background="#drawable/blue_line" />
<View
android:id="#+id/six_line"
android:layout_width="20dp"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/fifth_line"
android:background="#drawable/gray_line" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp">
<ImageView
android:id="#+id/wrd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:srcCompat="#drawable/logo_wrd" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:fontFamily="sans-serif-black"
android:text="Wireless Rehab Device"
android:textColor="#676767"
android:textSize="24sp" />
</RelativeLayout>
</LinearLayout>
The activity I showed Startscreen.java
public class Startscreen extends AppCompatActivity {
private static int SPLASH_DURTION_OUT = 5000; // 5000ms = 5s
//Views
View first, second, third, fourth, fifth, sixth;
TextView bottomTag;
ImageView wrd;
//Animations
Animation topAnimation, bottomAnimation, middelAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_startscreen);
topAnimation = AnimationUtils.loadAnimation(this, R.anim.top_animation);
bottomAnimation = AnimationUtils.loadAnimation(this, R.anim.bottom_animation);
middelAnimation = AnimationUtils.loadAnimation(this, R.anim.middel_animation);
first = findViewById(R.id.first_line);
second = findViewById(R.id.second_line);
third = findViewById(R.id.third_line);
fourth = findViewById(R.id.fourth_line);
fifth = findViewById(R.id.fifth_line);
sixth = findViewById(R.id.six_line);
wrd = findViewById(R.id.wrd);
bottomTag = findViewById(R.id.tag);
//Animation settings
first.setAnimation(topAnimation);
second.setAnimation(topAnimation);
third.setAnimation(topAnimation);
fourth.setAnimation(topAnimation);
fifth.setAnimation(topAnimation);
sixth.setAnimation(topAnimation);
wrd.setAnimation(middelAnimation);
bottomTag.setAnimation(bottomAnimation);
//Splash Screen
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(Startscreen.this, Home.class); //Home = Mainactivity
startActivity(intent);
finish();
}
}, SPLASH_DURTION_OUT);
}
}
This is my Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wrd">
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".activitys.Home" />
<activity android:name=".activitys.Startscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ble.BLE_Service"
android:enabled="true" />
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
Found the issue, all my .png images was in my Drawable-v24 folder and not Drawable, so it's fixed now!
i am working on a filter app and have created a splash screen and one other activity named 'ControlActivity' but as soon as the splash screen disappears the app closes without loading the 'ControlActivity'.
Also it does not gives any error closing the app.
The code for Main Activity is:-
package com.example.harsh.filters;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);
}
},1000);
}
}
The XML code for the ControlActivity:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:background="#android:color/white"
tools:context=".ControlActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:padding="12dp"
android:theme="?attr/actionBarTheme" />
<ImageView
android:id="#+id/centerImageView"
android:layout_width="match_parent"
android:layout_height="315dp"
android:layout_alignParentTop="true"
android:layout_marginTop="55dp"
app:srcCompat="#mipmap/img1"
tools:layout_below="#+id/toolbar"
tools:padding="16dp" />
<LinearLayout
android:id="#+id/controlLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/centerImageView"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp"
tools:layout_below="#+id/centerImageView"
tools:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/uncheck" />
<SeekBar
android:id="#+id/seekBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/check" />
</LinearLayout>
<LinearLayout
android:id="#+id/thumbnailLinearLayout"
android:layout_width="match_parent"
android:layout_height="147dp"
android:orientation="horizontal"
android:padding="8dp"
tools:layout_below="#+id/controlLinearLayout">
<ImageView
android:id="#+id/imageView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect1" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect2" />
<ImageView
android:id="#+id/imageView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect3" />
<ImageView
android:id="#+id/imageView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect4" />
</LinearLayout>
</RelativeLayout>
**The ControlActivity:-**
package com.example.harsh.filters;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class ControlActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
}
}
Manifest:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.harsh.filters">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ControlActivity"
android:label="#string/title_activity_control"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
Please help me out and thanks.
I have first activity from which I start another one.
I created second one with:
Right click on app inside project explorer
New > Activity > Empty Activity
and with this I got class file inside Java folder and activity_newActivity.xml
I have created some stuffs inside that activity and struggling why functions are not working when I try to run them from my class but then I tried to add Log.d("Testing", "TEST") inside onCreate function and what I have found out is that it doesn't even run it. Why is this happening?
Whole class:
package rs.termodom.www.termodom;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.widget.ImageView;
import android.widget.TextView;
public class pocetna extends AppCompatActivity {
Display display;
Point screenSize;
TextView tp_txt;
ImageView test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pocetna);
Log.d("PROBLEM", "PROBLEM");
//==========================================================================================
//==========================================================================================
//==========================================================================================
display = getWindowManager().getDefaultDisplay();
display.getSize(screenSize);
tp_txt = (TextView) findViewById(R.id.tp_txt);
tp_txt.setText("StA");
test = (ImageView)findViewById(R.id.akcija1_img);
test.getLayoutParams().height = 150;
test.getLayoutParams().width = screenSize.x;
//==========================================================================================
//==========================================================================================
//==========================================================================================
}
}
Here is manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rs.termodom.www.termodom">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".pocetna"
android:theme="#style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>
and XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="rs.termodom.www.termodom.pocetna">
<android.support.constraint.ConstraintLayout
android:id="#+id/header"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#android:color/holo_blue_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/userName_txt"
android:layout_width="151dp"
android:layout_height="32dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="KORISNIK 123"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tp_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="1600 TP"
android:textColor="#android:color/holo_red_dark"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:id="#+id/akcijaSlider"
android:layout_width="411dp"
android:layout_height="150dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="#+id/akcija1_img"
android:layout_width="268dp"
android:layout_height="112dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
Did you register the activity in application manifest (AndroidManifest.xml) file?
I think that the problem is here
display.getSize(screenSize);
The screenSizeobject isn't initialize.
Try this:
screenSize = new Point();
display = getWindowManager().getDefaultDisplay();
display.getSize(screenSize);
Problem was I wasn't calling new activity properly.
I should have called it like this:
Intent myIntent = new Intent(getApplicationContext(), pocetna.class);
startActivity(myIntent);
The Intent is working for some activity's, for some specific activity intent is crashing I have changed and tried it in different ways but for some activity intent is not working, app is getting built but after running on the phone it does not show any error during the debugging time ? I tried it in different way but it still crashes can anyone help me solve it?
MainActivity java
import android.content.Intent;
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 conee = (Button)findViewById(R.id.b1);
conee.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
});
Button cupp = (Button)findViewById(R.id.b2);
cupp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ko = new Intent(MainActivity.this,Main3Activity.class);
startActivity(ko);
}
});
}
}
Main3Activity java
package cheercreams.design;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Main3Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
}
MainActivity Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="cheercreams.design.MainActivity">
<Button
android:id="#+id/b1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"
android:layout_x="25dp"
android:layout_y="17dp"
android:text="Cone"
android:textSize="14dp" />
<Button
android:id="#+id/b2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#+id/b1"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:text="cup"
android:textSize="14dp" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/b3"
android:layout_toRightOf="#+id/b2"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:text="Butterfly box"
android:textSize="14dp"/>
<Button
android:id="#+id/b4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"
android:layout_below="#+id/b1"
android:text="family pack"
android:textSize="14dp" />
<Button
android:id="#+id/b5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#+id/b4"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b2"
android:text="kulfi"
android:textSize="14dp" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/b6"
android:layout_toRightOf="#+id/b5"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b3"
android:text="stick"
android:textSize="14dp"/>
<Button
android:id="#+id/b7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"
android:layout_below="#+id/b4"
android:text="sundae"
android:textSize="14dp" />
<Button
android:id="#+id/b8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#+id/b4"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b5"
android:text="special pack"
android:textSize="14dp" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/b9"
android:layout_toRightOf="#+id/b5"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b6"
android:text="cassette"
android:textSize="14dp"/>
</RelativeLayout>
Main3Activity Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="cheercreams.design.Main3Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/heading"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:id="#+id/t1"
android:layout_marginTop="20dp"/>
<ImageView
android:layout_width="240dp"
android:layout_height="200dp"
android:id="#+id/i1"
android:layout_below="#+id/t1"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:background="#drawable/bs"
android:layout_toRightOf="#+id/i2"/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="45dp"
android:numStars="5"
android:id="#+id/r1"
android:rating="4.5"
android:contextClickable="true"
android:layout_marginLeft="60dp"
android:layout_below="#+id/i1"
/>
<TextView
android:layout_width="60dp"
android:layout_height="30dp"
android:text="Type :"
android:textSize="20dp"
android:id="#+id/t4"
android:layout_below="#+id/r1"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="60dp"
android:layout_height="30dp"
android:text="Price:"
android:textSize="20dp"
android:id="#+id/t5"
android:layout_below="#+id/t4"
android:layout_marginLeft="60dp"
android:layout_marginTop="25dp"
/>
<TextView
android:layout_width="40dp"
android:layout_height="30dp"
android:id="#+id/t6"
android:text="#string/Rs"
android:textSize="20dp"
android:layout_below="#+id/b1"
android:layout_toRightOf="#+id/t5"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/b1"
android:text="500ml"
android:layout_below="#+id/r1"
android:layout_toRightOf="#+id/t4"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/b2"
android:text="1000ml"
android:layout_below="#+id/r1"
android:layout_toRightOf="#+id/b1"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"/>
<ImageButton
android:layout_width="80dp"
android:layout_height="70dp"
android:id="#+id/i2"
android:layout_below="#+id/t1"
android:background="#drawable/bs"
android:layout_marginTop="60dp"
android:layout_marginLeft="2dp"
/>
<ImageButton
android:layout_width="80dp"
android:layout_height="70dp"
android:id="#+id/i3"
android:layout_marginTop="2dp"
android:layout_below="#+id/i2"
android:layout_marginLeft="2dp"
android:background="#drawable/bs"
/>
<Button
android:layout_width="220dp"
android:layout_height="50dp"
android:id="#+id/b3"
android:layout_below="#+id/t5"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
android:text="Add to Cart"
android:background="#color/colorOrange"
/>
</RelativeLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cheercreams.design">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity" />
<activity android:name=".Main3Activity" />
<activity android:name=".Main4Activity" />
<activity android:name=".Main5Activity"></activity>
</application>
</manifest>
You defined Main2Activity in manifest file but you don't initiate Main2Activity class in package.
So, You able to go only from MainActivity to Main3Activity but not to Main2Activity.
The Start class:
It as a very simple program, i designed two screen, and by pressing a button on the main screen i wanted the app to open the second screen, but unfortunately its not happening, the app keeps crushing over and over again.
package com.example.snakesnladders;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Start extends Activity implements OnClickListener {
Button start, settings;
TextView snakes, and, ladders;
ImageView snakePic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
init();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
private void init() {
start = (Button) findViewById(R.id.btStart);
settings = (Button) findViewById(R.id.btSettings);
snakes = (TextView) findViewById(R.id.tvSnakes);
and = (TextView) findViewById(R.id.tvAnd);
ladders = (TextView) findViewById(R.id.tvLadders);
snakePic = (ImageView) findViewById(R.id.snakePic);
start.setOnClickListener(this);
settings.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btStart:
break;
case R.id.btSettings:
Intent i = new Intent("com.example.snakesnladders.SET");
startActivity(i);
break;
default: break;
}
}
}
The Set class:
package com.example.snakesnladders;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Set extends Activity implements OnClickListener {
Button sound, difficulty, back;
TextView settings;
ImageView snakePic;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.setscreen);
init();
}
private void init() {
sound = (Button) findViewById(R.id.btSound);
difficulty = (Button) findViewById(R.id.btDifficulty);
back = (Button) findViewById(R.id.btBack);
settings = (TextView) findViewById(R.id.tvSetPage);
snakePic = (ImageView) findViewById(R.id.setSnakePic);
sound.setOnClickListener(this);
difficulty.setOnClickListener(this);
back.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.btSound:
String s = sound.getText().toString();
if (s.equals("Sound:on")) {
sound.setText("Sound:off");
ControlSounds.player.stop();
} else {
sound.setText("Sound:on");
ControlSounds.player.start();
}
break;
case R.id.btDifficulty:
break;
case R.id.btBack:
Intent i = new Intent(Set.this, Start.class);
startActivity(i);
finish();
break;
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.snakesnladders"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Start"
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=".Set"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.snakesnladders.SET" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
mainscreen.xml:
<?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:background="#color/black"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="175dp"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSnakes"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Snakes"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textSize="30sp" />
<TextView
android:id="#+id/tvAnd"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="#string/and"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textSize="30sp" />
<TextView
android:id="#+id/tvLadders"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Ladders"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/green"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="125dp"
android:orientation="vertical" >
<Button
android:id="#+id/btStart"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Start New Game"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button
android:id="#+id/btSettings"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Settings"
android:textColor="#FFFFFF"
android:textSize="30sp" />
</LinearLayout>
<ImageView
android:id="#+id/snakePic"
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_gravity="center"
android:layout_weight="0.47"
android:background="#color/black"
android:src="#drawable/snake" />
</LinearLayout>
setscreen.xml:
<?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:background="#color/black"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSetPage"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center"
android:text="Settings"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:orientation="vertical" >
<Button
android:id="#+id/btSound"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Sound:on"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button
android:id="#+id/btDifficulty"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Difficulty:easy"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button
android:id="#+id/btBack"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Back To Menu"
android:textColor="#FFFFFF"
android:textSize="30sp" />
</LinearLayout>
<ImageView
android:id="#+id/setSnakePic"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.25"
android:background="#color/black"
android:src="#drawable/snake1" />
</LinearLayout>
Change this
Intent i = new Intent("com.example.snakesnladders.SET");
startActivity(i);
To
Intent i = new Intent(Start.this,Set.class);
startActivity(i);
And Change this
<activity
android:name=".Set"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.snakesnladders.SET" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
To
<activity
android:name=".Set"
android:label="#string/app_name" >
</activity>
Use Explicit Intent's
To know why read
http://developer.android.com/guide/components/intents-filters.html
Explicit intents specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent
to start a component in your own app, because you know the class name
of the activity or service you want to start. For example, start a new
activity in response to a user action or start a service to download a
file in the background.