my button crashes my project in eclipse android : ActivityNotFoundException - java

Answered(at bottom)
So I have been trying to get my button to allow me to go to the next screen.
I am programming in eclipse.
It is a basic login button to allow me to go to the login screen.
I apologize for the long post, but I feel that giving all the information I have right now is good.
I am wondering if it is my device I am using it to run on when I press play.
The error at the bottom of my logcat is:
(The first few lines of logcat)
11-06 18:08:59.973: PID 324 TID 324 D/AndroidRuntime(324): Shutting down VM
11-06 18:08:59.973: W/dalvikvm(324): threadid=1: thread exiting with uncaught exception
(group=0x4001d800)
11-06 18:08:59.983: E/AndroidRuntime(324): FATAL EXCEPTION: main
11-06 18:08:59.983: E/AndroidRuntime(324): android.content.ActivityNotFoundException: Unable to
find explicit activity class {com.example.coffeeshop/com.example.coffeeshop.Login}; have you
declared this activity in your AndroidManifest.xml?
Those are the first few lines as it goes to error.
So my res/layout/activity_main code:
(Please note that the second button for "Create account" is not going to be an issue I am trying to get the login to go to the login screen.)
<?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: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=".Main"
android:background="#15BCA8"
android:id="#+id/activity_main">
<Button
android:id="#+id/createbtn"
android:layout_width="2200dp"
android:layout_height="70dp"
android:padding="10dp"
android:textSize="25sp"
android:textStyle="bold|normal"
android:text="#string/create_account"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="44dp"
android:background="#drawable/roundedbutton" />
<Button
android:id="#+id/loginbtn"
android:layout_width="2200dp"
android:layout_height="80dp"
android:layout_above="#+id/createbtn"
android:layout_alignStart="#+id/createbtn"
android:layout_alignLeft="#+id/createbtn"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="29dp"
android:padding="10dp"
android:text="#string/log_in"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold|normal"
android:typeface="sans"
android:background="#drawable/roundedbutton"
/>
</RelativeLayout>
And this layout is suppose to lead to the login.xml. Also, the roundedbutton will be posted at the end, but I don't think it's an issue.
login.xml
<?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:background="#15BCA8"
android:id="#+id/loginlayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp"
android:text="Login"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/button"
android:textColor="#000000"
android:background="#15BCA8"
android:layout_marginTop="65dp"
android:layout_toStartOf="#+id/textView"
android:layout_below="#+id/textView"
android:layout_toLeftOf="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/button2"
android:textColor="#000000"
android:background="#15BCA8"
android:layout_marginTop="58dp"
android:layout_below="#+id/editText"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_below="#+id/button"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2"
android:background="#FFFFFF" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2"
android:background="#FFFFFF" />
<Button
android:id="#+id/sbmtbtn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/editText2"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/editText2"
android:layout_marginTop="20dp"
android:background="#drawable/roundedbutton"
android:text="Submit"
android:textSize="24sp" />
</RelativeLayout>
So this seems relatively fine, yeah?
Here is my MainActivity.java class:
package com.example.coffeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.loginbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Login.class);
startActivity(intent);
}
});
}
}
And should link me to my login screen, coded here:
package com.example.coffeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Login extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.sbmtbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MainMenu.class);
startActivity(intent);
}
});
}
}
And for fun, here is my "roundedbutton.xml" file being called upon by my xml files.
Which shouldn't have an issue here.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- solid android:color="#834672"-->
<gradient android:startColor="#834672"
android:endColor="#718C93"
android:angle="270" />
<stroke android:width="5px" android:color="#06302F" />
<corners android:radius="10dp"/>
</shape>
Now I can not understand why my program crashes.
Ok. I now have an answer that has worked out for me. I was not aware of the manifest, which was instantly solved with the code that "fast snail" left in his answer.

based on your logcat, the problem is that you haven't defined your activity in manifest file.
The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code. before the app runs the system must know which activities are there in your app, and it knows via manifest file. and if you miss any activity in manifest file then system assumes that the activity doesnt exist. now, when app runs it points to some activity that is not recognized by the system. hence, it crashes. you can read more about android manifest file here http://developer.android.com/guide/topics/manifest/manifest-intro.html
i cannot upvote else i would go with #fast snail's answer,i think that will solve your problem.

well
ActivityNotFoundException
that mean you have not declare this activity in AndroidManifest file.you need to declare all activities in manifest file
error exactly says you the problem
find explicit activity class {com.example.coffeeshop/com.example.coffeeshop.Login}; have you
declared this activity in your AndroidManifest.xml?
add this line to manifest file
<activity android:name=".Login"></activity>
finaly manifest should looks like// example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.coffeeshop">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity" android:label="#string/app_name"></activity>
<activity android:name=".Login"></activity> <!--add this line -->
</application>
</manifest>

Related

Crashes probably due to bad activity layout

I've been developing an application and it has been working good until I added a LinearLayout (id = 'toolbar') and some functions in that activity's corresponding Java file. Here are my codes:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.myapplication">
<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=".EditPhotoActivity"
android:parentActivityName=".MainActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
MainActivty is working good and I don't think the problem is in it. Please let me know if you want me add its code.
EditPhotoActivity.java
package com.example.com.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.io.IOException;
public class EditPhotoActivity extends AppCompatActivity {
public ImageView image_view = findViewById(R.id.image_display) ;
public boolean toolbar_is_open = false ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_photo);
ImageView imageView = findViewById(R.id.image_display);
Intent intent = getIntent();
Uri content;
if(intent.getStringExtra(MainActivity.DATA_TYPE).equals("Uri")) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
content = (Uri) bundle.get(MainActivity.IMAGE_KEY);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), content);
imageView.setImageBitmap(bitmap);
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(this, "Failed to fetch image data!", Toast.LENGTH_LONG).show() ;
}
}
} else if(intent.getStringExtra(MainActivity.DATA_TYPE).equals("Bundle")) {
Bundle bundle = intent.getBundleExtra(MainActivity.IMAGE_KEY);
Bitmap bitmap = (Bitmap)bundle.get("data");
imageView.setImageBitmap(bitmap);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.publish_menu, menu);
return true ;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.share_button:
// Save the photo
break;
case R.id.save_button:
// Open Share Photo Activity
break;
}
return super.onOptionsItemSelected(item);
}
private void setToolbarState(boolean open) {
LinearLayout toolbar = findViewById(R.id.toolbar);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)toolbar.getLayoutParams();
params.height = open? 50 : 0;
toolbar.setLayoutParams(params);
toolbar_is_open = open ;
}
public void openRotateToolbar(View view) {
Button right = new Button(this), left = new Button(this);
right.setText(R.string.right); left.setText(R.string.left);
right.setBackgroundResource(R.drawable.custom_button);
left.setBackgroundResource(R.drawable.custom_button);
right.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
image_view.setRotation(90f);
}
});
left.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
image_view.setRotation(-90f);
}
});
if(!toolbar_is_open)
setToolbarState(true);
}
public void closeToolbar(View view) {
if(toolbar_is_open)
setToolbarState(false);
}
}
activity_edit_photo.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="com.example.com.myapplication.EditPhotoActivity"
tools:layout_editor_absoluteY="81dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:weightSum="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_display"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="#color/black"
android:padding="0dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background"
tools:ignore="contentDescription" />
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#111"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#+id/scrollView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/close"
android:background="#drawable/custom_button"
android:textColor="#fff"
android:onClick="closeToolbar" />
</LinearLayout>
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/darkGrey"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:text="#string/add_sticker"
android:textColor="#fff"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="-15dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/create_sticker"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/cut_sticker"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/apply_filter"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/rotate"
android:textSize="12sp"
tools:ignore="ButtonStyle"
android:onClick="openRotateToolbar" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/flip"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/crop"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
</LinearLayout>
</HorizontalScrollView>
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
I am not getting any errors but when the app is transitioning to EditPhoto Activity it crashes. Please let me know if you need me to provide you with any other information.
Note: I can not post the logcat because I am not using ADB and I am not using a computer that is all mine (it's my family's) so I don't want to enable x-vt in BIOS and so I am not able to use an Emulator also. To test my app I build the apk then I install it on my phone.
I have found my mistake. I will let this answer here for anyone that comes into the same problem.
The line:
public ImageView image_view = findViewById(R.id.image_display) ;
causes NullPointerException because at the time the app is opening the activity it does not yet have all the view and id's. It gets them only when it calls onCreate so I replaced the line to
public ImageView image_view ;
and inside onCreate after the super.onCreate(savedInstanceState) and the setContentView(R.layout.activity_edit_photo) calls I added
image_view = findViewById(R.id.image_display) ;

Method is missing or has incorrect signature

I am making a simple app in which context view changes and then a toast message is displayed as input given by user. I don't understand why the app keeps on crashing when changing the context view.
Also Android studio gives this warning:
Method "Toaster" is missing in "FirstActivity" or has incorrect signature.
Here is my code:
activity_me_clicked.xml:
<?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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/welcome"
android:textSize="36sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:text="#string/intro"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginEnd="7dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/named"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="MainProcess"
android:text="#string/done" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
tools:ignore="UselessLeaf"></LinearLayout>
</LinearLayout>
</LinearLayout>
FirstActivity.Java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class FirstActivity extends AppCompatActivity {
public static String owner_string;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_clicked);
}
public void MainProcess(View view) {
final String TAG="DEBUG";
Log.d(TAG,"At least process started");
EditText owner = (EditText) findViewById(R.id.name);
owner_string = owner.getText().toString();
Log.d(TAG,"owner name stored");
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
setContentView(R.layout.activity_main_screen);
Log.d(TAG,"content shown");
}
}
activity_main_screen.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:orientation="vertical">
<TextView
android:id="#+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/what_would_you_like_me_to_do_today"
android:textSize="18sp" />
<Button
android:id="#+id/Cam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/camera" />
<Button
android:id="#+id/Mus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/music" />
<Button
android:id="#+id/QR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/scanQR" />
<EditText
android:id="#+id/toastText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/order"
android:inputType="textPersonName" />
<Button
android:id="#+id/toaster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:onClick="toaster"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/toast"
android:layout_marginEnd="100dp"
android:layout_gravity="center"/>
</LinearLayout>
MainScreen.java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
}
public void toaster(View view){
EditText toast = (EditText)findViewById(R.id.toastText);
String final_toast = toast.getText().toString();
Toast.makeText(getApplicationContext(), final_toast, Toast.LENGTH_SHORT).show();
}
}
EDIT: As suggested, I moved the toaser function to FirstActivity.Java and deleted the MainScreen.java file as it becomes pointless to keep it. But the major problem is when I press the button (id named) the app keeps stopping.
EDIT2: I found that setContentView(R.layout.activity_main_screen) in FirstActivity.Java needs to be above this code
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
so that Activity has access to all the layout components. Thanks solved :)
You replaced the content view in the first activity with a layout that include the onClick attribute, but you have no public void toaster(View view) method there.
So, either don't use setContentView a second time, or implement that method on both Activities.
The recommended way to replace the view is Fragments, by the way
This is a wrong way to change the contents of your Activity, The main problem here is that you have onClick attribute set to "toaster", And you don't have a function called "toaster" in your FirstActivity.
Beside that, The MainScreen Activity in your code will never be used.
So, Your problem is that you set the FirstActivity contents to "activity_main_screen.xml", And FirstActivity doesn't have "toaster" method in it, When you change the context, the Activity will try to find toaster method and the app will crash because the method doesn't exist in FirstActivity.
You can solve this problem by making a method called "toaster" inside FirstActivity, But
This is a very bad practice, You can use Fragments instead, Or you can use Intent to move to another activity.
Some useful links about Fragments:
https://developer.android.com/guide/components/fragments.html
https://www.tutorialspoint.com/android/android_fragments.htm
https://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/
Why should I use fragment in Android?

Android Studio: FATAL EXCEPTION: main while getting user inputs

I was trying to use user input name for later use but Once i run the app this is closed by saying unfortunately your app has stopped. I have provided Main activity,Layout xml and android manifest codes. Please anyone answer this . I am newbie in android and cant figure out the error.
Main_activity
package rupakthapa.droiddynasty.com.interactivestory2;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import static android.R.attr.duration;
import static android.R.attr.name;
public class MainActivity extends AppCompatActivity {
private EditText mNameField;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameField = (EditText) findViewById(R.id.nameField);
mButton = (Button) findViewById(R.id.startButton);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String a= mNameField.getText().toString();
Context context = getApplicationContext();
int length = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, a, length);
toast.show();
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
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"
tools:context="rupakthapa.droiddynasty.com.interactivestory2.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:id="#+id/imageView"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="149dp"
android:contentDescription="startImage"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/nameField"
android:hint="Enter YOur Name"
android:layout_above="#+id/startButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:maxLength="30"
android:layout_marginLeft="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<Button
android:text="Start your adventure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/startButton"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest package="rupakthapa.droiddynasty.com.interactivestory2"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
There is a problem here. You dont have EditText in your layout.
mNameField = (EditText) findViewById(R.id.nameField);
In your layout you should change TextView to EditText
<EditText
android:layout_width="match_parent"
android:id="#+id/nameField"
android:hint="Enter YOur Name"
android:layout_above="#+id/startButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:maxLength="30"
android:layout_marginLeft="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:textSize="16sp"/>

My application is working slowly on smartphones

i am a beginner programmer of Android, when i create an App and install it on my real phone or other phones it works very slowly (i dont talk about emulator).
- The size of file is only 3 MB
- This happens to all applications that i make
- I cleaned the cache and the project before building
the devices that i installed the application on : HTC630, Samsung galexy S7, Samsung Duos j1.
any idea ?
<?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"
android:background="#drawable/bcc">
<LinearLayout
android:layout_width="0dp"
android:layout_height="495dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/edge"
android:layout_marginTop="#dimen/edge"
android:text="Enter your name:"
android:textColor="#FFFFFF"
android:textSize="#dimen/fsize"
android:textStyle="bold"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/edge"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#FFFFFF"
android:textSize="#dimen/fsize"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/edge"
android:layout_marginRight="#dimen/edge"
android:onClick="nxt"
android:text="Next"
android:textSize="#dimen/fsize"
tools:layout_editor_absoluteX="38dp"
tools:layout_editor_absoluteY="0dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
java code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void nxt(View view) {
EditText name =(EditText)findViewById(R.id.editText2);
Intent txtint= new Intent(this,Main2Activity.class);
Bundle b = new Bundle();
b.putString("user",name.getText().toString());
txtint.putExtras(b);
startActivity(txtint);
}
}
Finally i solved the problem.. i had a mistake in resize of background (to fit screens).

Eclipse: 'ClassCastException' and GUI is not showing in Graphical Layout

I have created a custom component. Here is the code
html_5_spinner.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:orientation="horizontal" >
<EditText
android:id="#+id/spinText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/upBtn"
android:layout_width="25dp"
android:layout_height="15dp"
android:background="#android:drawable/arrow_up_float" />
<Button
android:id="#+id/downBtn"
android:layout_width="25dp"
android:layout_height="15dp"
android:background="#android:drawable/arrow_down_float" />
</LinearLayout>
</LinearLayout>
HTML5Spinner.java
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class HTML5Spinner extends LinearLayout {
private EditText spinText;
private Button upBtn, downBtn;
public HTML5Spinner(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
protected void onFinishInflate()
{
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.html5_spinner, this);
setupViewItems();
}
private void setupViewItems()
{
spinText = (EditText)findViewById(R.id.spinText);
upBtn = (Button)findViewById(R.id.upBtn);
downBtn = (Button)findViewById(R.id.downBtn);
//Register Listeners
upBtn.setOnClickListener(new UpButtonAction());
}
private class UpButtonAction implements OnClickListener
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("workn", "It is running");
}
}
}
Main activity is using the above cusom component. It just has a XML, no business logic or GUI rendering Java code
activity_main.xml
<ScrollView
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:drawablePadding="5dp"
android:text="RadioButton" />
<com.nFlate.nflate.HTML5Spinner
android:id="#+id/aasaas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></com.nFlate.nflate.HTML5Spinner>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:drawablePadding="5dp"
android:text="RadioButton" />
<com.nFlate.nflate.HTML5Spinner
android:id="#+id/aasaas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></com.nFlate.nflate.HTML5Spinner>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
This gives no error when running. But eclipse shows errors in "Graphical Layout" of activity_main.xml
Below is the error
Exception raised during rendering: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity
Exception details are logged in Window > Show View > Error Log
java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity
at com.xxx.xxx.HTML5Spinner.onFinishInflate(HTML5Spinner.java:27)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:754)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
Below is the place where this error is displayed.
Because of this error, the GUI is not getting displayed in eclipse Graphical Layout. Why is this?
In your onFinishInflate there's this problematic cast:
((Activity)getContext())
Eclipse Graphical Layout renders views with a Context that is not an Activity. You shouldn't need this cast here anyway.
You are missing the library which renders the spinner. Include that library in your project. Please import library into the workspace in order to work properly and mark this library in the android option of the project properties. The main problem for this is not including the library properly.
if in your manifest file the HTML5Spinner.java registered as a activity then it must extends Activity type things to proceed but here the LinearLayout used so thins would not start..so that the xml GUI aslo giving error for ClassCastException..

Categories

Resources