Public class must be defined in its own file - android

I am trying to use two views in one application to come up with the user's planetary weight. After rewriting the java several times, I finally have it working... mostly, but on the public class Planets it tells me "The public type planets must be defined in its own file." I went into the manifest and made an activity for it, but that didn't help anything. Planets is already the name of one of my xml files. How do I make the public type into its own file?
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" >
<TextView
android:id="#+id/askwtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="19dp"
android:text="#string/askwt" />
<EditText
android:id="#+id/inputwtEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/askwtTextView"
android:layout_below="#+id/askwtTextView"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/inputwtEditText"
android:layout_below="#+id/inputwtEditText"
android:layout_marginTop="38dp"
android:onClick="buttonclick"
android:text="#string/enter" />
</RelativeLayout>
Planets.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/planetTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/planet" />
<TextView
android:id="#+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/mercuryRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/mercury" />
<RadioButton
android:id="#+id/venusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/venus" />
<RadioButton
android:id="#+id/earthRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/earth" />
<RadioButton
android:id="#+id/marsRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/mars" />
<RadioButton
android:id="#+id/jupiterRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/jupiter" />
<RadioButton
android:id="#+id/saturnRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/saturn" />
<RadioButton
android:id="#+id/uranusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/uranus" />
<RadioButton
android:id="#+id/neptuneRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/neptune" />
<RadioButton
android:id="#+id/plutoRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pluto" />
</RadioGroup>
<Button
android:id="#+id/selectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonclick2"
android:text="#string/select" />
<TextView
android:id="#+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout
JAVA:
package com.deitel.planetaryweight;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.*;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends Activity {
/* You should get used to declaring everything with the correct visibility. Good practice is to make everything private and use public mutator methods */
//Global variable
private double weight;
private Button enter; // creates a button
// Views
private EditText wtEntry;
private TextView txtForm2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Start with first screen
setContentView(R.layout.activity_main);
enter = (Button) findViewById(R.id.enterButton);
//creates an editext and assigns the resource id of the xml edittext.
wtEntry = (EditText)findViewById(R.id.inputwtEditText);
txtForm2 = (TextView)findViewById(R.id.textViewform2);
}
// Button clicks shouldn't do anything but perform clicky actions. Leave field initialization, view creation, etc to the Activity.
//buttonclick for form 1
public void buttonclick(View view){
//Receives the input from the edittext, converts it to a double (number).
weight = Double.parseDouble(wtEntry.getText().toString());
TextView t2 = null;
//change the value of the textview on screen 2 to the calculation value
t2.setText(Double.toString(weight));
// If you want a new layout, it's best to start a new activity.
// It looks like you want to get information back, so use startActivityForResult().
// setContentView(R.layout.planets);
Intent dataIntent = new Intent(this, Planets.class);
dataIntent.putExtra("com.deitel.identifier.DATA_WEIGHT", weight);
startActivityForResult(dataIntent, Activity.RESULT_OK);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check that the resultCode is the same as we started the activity with
if(resultCode == Activity.RESULT_OK){
// get the double from the Intent, using the same string name (package prefixed)
// or a default value if it didn't get set.
double resultWeight = data.getDoubleExtra("com.deitel.identifier.RESULT_WEIGHT", 0.0);
// Now do something with resultWeight
}
}
}
// PlanetChooser.class
public class Planets extends Activity {
// constants, usually denoted by uppercase and declared static and final
public static final double MERCURYFORCE = 0.38;
public static final double VENUSFORCE = 0.91;
public static final double EARTHFORCE = 1.00;
public static final double MARSFORCE = 0.38;
public static final double JUPITERFORCE = 2.34;
public static final double SATURNFORCE = 1.06;
public static final double URANUSFORCE = 0.92;
public static final double NEPTUNEFORCE = 1.19;
public static final double PLUTOFORCE = 0.06;
private RadioButton mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto;
// No need to use the Double object as opposed to the primitive unless you have good reason
private double mercurypf, venuspf, earthpf, marspf, jupiterpf, saturnpf, uranuspf, neptunepf, plutopf, weight;
// One variable will suffice, it seems.
private double resultForce;
public void onCreate(Bundle s){
super.onCreate(s);
setContentView(R.layout.planets);
mercury = (RadioButton) findViewById(R.id.mercuryRadio);
venus = (RadioButton) findViewById(R.id.venusRadio);
earth = (RadioButton) findViewById(R.id.earthRadio);
mars = (RadioButton) findViewById(R.id.marsRadio);
jupiter = (RadioButton) findViewById(R.id.jupiterRadio);
saturn = (RadioButton) findViewById(R.id.saturnRadio);
uranus = (RadioButton) findViewById(R.id.uranusRadio);
neptune = (RadioButton) findViewById(R.id.neptuneRadio);
pluto = (RadioButton) findViewById(R.id.plutoRadio);
}
public void buttonclick2(View view){
/*
It looks to me here you're looking to see which box is checked, and set a value based on
that planet. Since instance variables (in this case mercurypf, jupiterpf, etc) are initialized
to the default value (0), there's no need to set them manually.
*/
// Code used to determine which planet RadioButton is checked:
if(mercury.isChecked()) {
resultForce = MERCURYFORCE * weight;
}
if(venus.isChecked()){
resultForce = VENUSFORCE * weight;
}
if(earth.isChecked()){
resultForce = EARTHFORCE * weight;
}
if(mars.isChecked()){
resultForce = MARSFORCE * weight;
}
if(jupiter.isChecked()){
resultForce =JUPITERFORCE * weight;
}
if(saturn.isChecked()){
resultForce = SATURNFORCE * weight;
}
if(uranus.isChecked()){
resultForce = URANUSFORCE * weight;
}
if(neptune.isChecked()){
resultForce = NEPTUNEFORCE * weight;
}
if(pluto.isChecked()){
resultForce = PLUTOFORCE * weight;
}
// Create a new data Intent to pass back to the calling activity, set the result code,
// and manually finish() this activity.
Intent dataIntent = new Intent(this, null);
dataIntent.getDoubleExtra("com.deitel.identifier.RESULT_DATA", resultForce);
setResult(Activity.RESULT_OK, dataIntent);
finish();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deitel.planetaryweight"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Planets" android:label="#string/title_planets"></activity>
</application>
</manifest>

Your Java file is named MainActivity.java (the name of the public class it defines). Remove the Planets class from it, and put that class into a Planets.java file. That's pretty much just how Java wants it to be.

Related

StartActivityForResult not working properly from second activity

I am a noob at this, so going to keep it short.
This is a practice app which sends data from first activity to second activity as a message and then the first activity receives data from the second activity as a message. The tutorial I'm following instructed me to use the StartActivityForResult() function to extract data from a second activity to the first activity. I have two questions:
What is wrong with this code:
I. The MainActivity.java file
package com.example.android.twoactivitiesredo;
import android.content.Intent;
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 MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
//LOG_TAG contains the name of the class package, encapsulated for ease
public static final String EXTRA_MESSAGE =
"com.example.android.twoactivities.extra.MESSAGE";
//This will be used as the unique key to send data to the second Activity
public static final int TEXT_REQUEST = 1;
//This is used to define the key for a particular type of response that
you're interested in.
private EditText mMessageEditText;
//This EditText is used to send the message to the second activity
private TextView mReplyHeadTextView;
private TextView mReplyTextView;
//These private variables hold the reply header and the reply TextViews
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessageEditText = (EditText) findViewById(R.id.editText_main);
mReplyHeadTextView = (TextView) findViewById(R.id.textHeaderReply);
mReplyTextView = (TextView) findViewById(R.id.textMessageReply);
}
public void launchSecondActivity(View view) {
Log.d(LOG_TAG, "Button Clicked!");
Intent intent = new Intent(this, SecondActivity.class);
String message = mMessageEditText.getText().toString();
//Gets the data from EditText, converts it to String, and stores it in message
intent.putExtra(EXTRA_MESSAGE,message);
startActivityForResult(intent, TEXT_REQUEST);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == TEXT_REQUEST){
if(resultCode == RESULT_OK){
String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
mReplyHeadTextView.setVisibility(View.VISIBLE);
mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
}
}
}
}
II. The SecondActivity.java file
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity{
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
//This tag will be used as a key to send reply to first activity
private EditText mReply;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
//Gets the intent that activated this activity
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//This extracts the extra text sent along with this key String, thus
receiving the text
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
mReply = (EditText) findViewById(R.id.replyEditText);
//Do not reuse the intent from the first activity, create a new one
Intent replyIntent = new Intent();
setResult(RESULT_OK,replyIntent);
//RESULT_OK has the value -1 and is used as a Result code in the Activity
class
//to check that the data is send without a complication
finish();
}
public void returnReply(View view) {
}
}
III. The activity_main.xml file
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="#string/sendButton"
android:onClick="launchSecondActivity"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="#id/button_main"
android:layout_toLeftOf="#+id/button_main"
android:hint="Enter Your Message Here"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textHeaderReply"
android:text="#string/textHeaderReply"
android:visibility="invisible"/>
<!--The visibility mode is used to select how the attribute will be before data is passed to it.-->
<!--Invisible means that the attribute will be invisible before data is passed to it, hence not confusing the user-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textMessageReply"
android:layout_below="#+id/textHeaderReply"
android:visibility="invisible"/>
IV. The activity_second.xml file
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_header"
android:layout_marginBottom="20dp"
android:text="#string/text_header"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="#string/sendButton"
android:onClick="launchSecondActivity"/>
<TextView
android:id="#+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_header"
android:layout_margin="10dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/replyEditText"
android:onClick="returnReply"
android:layout_toLeftOf="#+id/button_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:hint="#string/replyText"
/>
</RelativeLayout>
V. The AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.twoactivitiesredo">
<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=".SecondActivity"
android:label="#string/secondActivity"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.twoactivitiesredo.MainActivity"/>
</activity>
</application>
</manifest>
I am sorry if the code is taking a lot of space, but I really needed help in clarifying my doubts and problems here, about why this code isn't working.
Thanks in advance to all the answers.
Replace your secondActivity.java with this:
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity{
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
//This tag will be used as a key to send reply to first activity
private EditText mReply;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
mReply = (EditText) findViewById(R.id.replyEditText);
}
public void returnReply(View view) {
Intent replyIntent = new Intent();
String replyMessage = mReply.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, replyMessage);
setResult(RESULT_OK,replyIntent);
finish();
}
}
In activity_second.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_header"
android:layout_marginBottom="20dp"
android:text="#string/text_header"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Reply Button"
android:onClick="returnReply"
/>
<TextView
android:id="#+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_header"
android:layout_margin="10dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/replyEditText"
android:layout_toLeftOf="#+id/button_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:hint="#string/replyText"
/>
</RelativeLayout>

How to Display Text in new row in the TextView when you press the button

Goal:
when you press the button a new line with new text should be displayed in textview.
Problem:
Based on source code it is not working in order to fulfill the goal.
What part am I missing?
Info:
*I'm new in android
*I'm using API 23
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.netvork">
<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>
</application>
</manifest>
activity_main.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="com.jfdimarzio.netvork.MainActivity">
<Button
android:id="#+id/btn_send"
android:layout_width="349dp"
android:layout_height="54dp"
android:layout_alignEnd="#+id/edittxt_input"
android:layout_alignParentBottom="true"
android:layout_marginBottom="12dp"
android:onClick="inputData"
android:text="Send"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="441dp" />
<EditText
android:id="#+id/edittxt_input"
android:layout_width="348dp"
android:layout_height="39dp"
android:layout_above="#+id/btn_send"
android:layout_alignStart="#+id/txtview_display"
android:layout_marginBottom="19dp"
android:ems="10"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="392dp" />
<TextView
android:id="#+id/txtview_display"
android:layout_width="348dp"
android:layout_height="355dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" />
</RelativeLayout>
package com.jfdimarzio.netvork;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static com.jfdimarzio.netvork.R.id.txtview_display;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void inputData(View view)
{
EditText editText = (EditText) findViewById(R.id.edittxt_input);
String message = editText.getText().toString();
TextView myTextView = (TextView) findViewById(R.id.txtview_display);
myTextView.setText(message + "\n");
}
}
I think you want to display the content of editText in a new line along with the previous text. If so, you may change the function inputData() like this:
public void inputData(View view) {
EditText editText = (EditText) findViewById(R.id.edittxt_input);
String message = editText.getText().toString();
TextView myTextView = (TextView) findViewById(R.id.txtview_display);
String oldText = myTextView.getText();
myTextView.setText(oldText + "\n" + message);
}
Actually when you want setText right now from the activity , you have to refresh Activity and after that is not work becouse the string not saved by SharedPreferences. or you can create a method like this and add SharedPreferences
public class MainActivity extends AppCompatActivity {
private String message;
private TextView myTextView;
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.txtview_display);
editText = (EditText) findViewById(R.id.edittxt_input);
}
public void inputData(View view) {
message = editText.getText().toString().trim();
if (!message .isEmpty()){
MethodName(message);
}
}
public void MethodName(String message){
myTextView.setText(message + "\n");
}
}

Android: When I try to install my signed app, it says "App not installed"

When I try to install a signed apk file, the application installer says "App not installed".
It happens to every app that I have made.
Even if I create a brand new keystore, or if I set the build mode to debug.
Although it does work if I install it through the Android Studio. But if I need to share it with my friends I can't.
I tried to look for a question that can appeal my situation on this site, but I haven't found any.
My application is working and (supposedly) signed. I can't find any reason for it not to work.
For example, this is the code for the JustJava app I made with the android development course at Udacity.
The activity_main.xml is:
<ScrollView 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="#+id/name"
android:hint="#string/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginBottom="16dp"/>
<TextView
style="#style/HeaderTextStyle"
android:text="#string/toppings" />
<CheckBox
android:id="#+id/topping_whipped_cream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:paddingLeft="24dp"
android:text="#string/topping_whipped_cream" />
<CheckBox
android:id="#+id/topping_chocolate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:paddingLeft="24dp"
android:text="#string/topping_chocolate" />
<TextView
style="#style/HeaderTextStyle"
android:text="#string/quantity" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<Button
android:id="#+id/decrement"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:onClick="decrement"
android:text="#string/minus" />
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:text="#string/quantity_num"
android:textColor="#android:color/black"
android:textSize="18sp" />
<Button
android:id="#+id/increment"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:onClick="increment"
android:text="#string/plus" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submitOrder"
android:text="#string/order_btn" />
</LinearLayout>
</LinearLayout>
And the Java is:
package com.example.android.justjava;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.NumberFormat;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* Assigns the name text field
*/
/**
* This method is called when the order button is clicked.
*/
int quantity = 1;
public void submitOrder(View view) {
EditText name = (EditText) findViewById(R.id.name);
String clientName = name.getText().toString();
Intent submit = new Intent(Intent.ACTION_SEND);
submit.setData(Uri.parse("mailto:"));
submit.setType("*/*");
submit.putExtra(Intent.EXTRA_CC, "roeyvi19#gmail.com");
submit.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.mail_subject_order) + clientName);
submit.putExtra(Intent.EXTRA_TEXT, createOrderSummary());
if (submit.resolveActivity(getPackageManager()) != null) {
startActivity(submit);
}
}
public void increment(View view) {
if (quantity < 99) {
quantity += 1;
displayQuantity(quantity);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
getString(R.string.over_100_coffees_toast), Toast.LENGTH_LONG);
toast.show();
}
}
public void decrement(View view) {
if (quantity == 1) {
Toast toast = Toast.makeText(getApplicationContext(), R.string.no_coffees_toast, Toast.LENGTH_SHORT);
toast.show();
} else {
quantity -= 1;
displayQuantity(quantity);
}
}
/**
* This method displays the given quantity value on the screen.
*
* #param quant
*/
private void displayQuantity(int quant) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + quant);
}
/**
* Creates a visual summary of the order.
* <p/>
* quantity is the number of cups of coffee ordered
*/
private String createOrderSummary() {
/**
* Assigns the whipped cream checkbox
*/
CheckBox toppingWhippedCream = (CheckBox) findViewById(R.id.topping_whipped_cream);
boolean isCheckedWhippedCream = toppingWhippedCream.isChecked();
/**
* Assigns the chocolate checkbox
*/
CheckBox toppingChocolate = (CheckBox) findViewById(R.id.topping_chocolate);
boolean isCheckedChocolate = toppingChocolate.isChecked();
//retrieve client's name
EditText name = (EditText) findViewById(R.id.name);
String clientName = name.getText().toString();
/**
* Inputs the total price
*/
int pricePerCup = 5;
if (isCheckedWhippedCream) {
pricePerCup += 1;
}
if (isCheckedChocolate) {
pricePerCup += 2;
}
int totalPrice = pricePerCup * quantity;
return //returns the name of the client
getString(R.string.name_at_order_summary) + clientName + "\n"
//return quantity
+ getString(R.string.quantity_at_order_summary) + quantity + "\n"
//return if whipped cream is checked
+ getString(R.string.topping_whipped_cream_at_order_summary) + isCheckedWhippedCream + "\n"
//return if chocolate is checked
+ getString(R.string.topping_chocolate_at_order_summary) + isCheckedChocolate + "\n"
//return total price
+ getString(R.string.total_price_at_order_summary) + totalPrice + "\n" +
getString(R.string.thank_you_at_order_summary);
}
}
Mainfest:
<?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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Thank you!
Check if in guests account(if you are emulating in android 5.0+) the app is installed or not, if yes then remove it and re-install from IDE.
Since you already have enabled installation from 'unknown sources'
Taking from 'App not Installed' Error on Android
This error also occurs when trying to install an apt on a phone with a previous version of the same apt and both asks aren't signed with the same certificate, so you might wanna check if you used it previously that you used the same certificate. One way to ensure this is to uninstall all previous versions and install the new signed apk

I have an error opening trace file and a unable to start activity compotentInfo and no fix seems to work, Android

Error(s):
10-17 18:53:39.298: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quizme/com.quizme.QuizMeActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView
10-17 18:53:38.747: error opening trace file: No such file or directory (2)
Upon trying to run it I get the unfortunatly, the app does not work error as well.
Java file:
package com.quizme;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class QuizMeActivity extends Activity {
private TextView question;
private TextView result;
private ImageButton forward;
private ImageButton backward;
private Button bTrue;
private Button bFalse;
private TrueFalse mTrueFalse;
/*
* onCreate(Bundle savedInstanceState)
* Instantiates all private GUI elements to their corresponding Views
* in activity_quiz_me.xml
* Sets the string values of the questions in the TrueFalse object,
* and sets their truth values.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_me);
Resources res = this.getResources(); //this is to be able to get strings
bTrue=(Button)findViewById(R.id.true_button);
forward=(ImageButton)findViewById(R.id.forward_button);
bFalse=(Button)findViewById(R.id.false_button);
backward=(ImageButton)findViewById(R.id.back_button);
result=(TextView)findViewById(R.id.result);
result.setText("");
question=(TextView)findViewById(R.id.question);
question.setText(mTrueFalse.getmQuestionSet()[0]);
mTrueFalse.setmQuestionSet(0, (String) res.getText(R.string.q1));
mTrueFalse.setmQuestionSet(1, (String) res.getText(R.string.q2));
mTrueFalse.setmQuestionSet(2, (String) res.getText(R.string.q3));
mTrueFalse.setmQuestionSet(3, (String) res.getText(R.string.q4));
mTrueFalse.setmQuestionSet(4, (String) res.getText(R.string.q5));
mTrueFalse.setmArrOfBols(0, false);
mTrueFalse.setmArrOfBols(1, true);
mTrueFalse.setmArrOfBols(2, true);
mTrueFalse.setmArrOfBols(3, false);
mTrueFalse.setmArrOfBols(4, true);
}
public void checkTrue(View view) {
Resources res = this.getResources(); //this is to be able to get strings
if(mTrueFalse.getCurrentBolValue()==true)
{
result.setText((String) res.getText(R.string.right));
}
else
{
result.setText((String) res.getText(R.string.wrong));
}
}
public void checkFalse(View view) {
Resources res = this.getResources(); //this is to be able to get strings
if(mTrueFalse.getCurrentBolValue()==true)
{
result.setText((String) res.getText(R.string.wrong));
}
else
{
result.setText((String) res.getText(R.string.right));
}
}
public void goForward(View view) {
if(mTrueFalse.getmCurrentQ()<mTrueFalse.getmQuestionSet().length-1)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmCurrentQ()+1);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
else if(mTrueFalse.getmCurrentQ()==mTrueFalse.getmQuestionSet().length-1)
{
mTrueFalse.setmCurrentQ(0);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
}
public void goBack(View view) {
if(mTrueFalse.getmCurrentQ()>0)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmCurrentQ()-1);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
else if(mTrueFalse.getmCurrentQ()==0)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmQuestionSet().length-1); //for the purposes of the project, its setting currentQ to 4
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz_me, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout xml 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:orientation="vertical"
android:id="#+id/activity_view"
tools:context="com.quizme.QuizMeActivity">
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sTrue"
android:layout_marginRight="180dp"
android:onClick="checkTrue"
/>
<ImageButton
android:id="#+id/forward_button"
android:contentDescription="#string/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/back_button"
android:onClick="goForward"
android:src="#drawable/forward" />
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/question"
android:layout_toRightOf="#+id/true_button"
android:onClick="checkFalse"
android:text="#string/sFalse" />
<ImageButton
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="70dp"
android:contentDescription="#string/back"
android:onClick="goBack"
android:src="#drawable/back" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/resultStr"
android:textSize="30sp" />
<TextView
android:id="#+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/true_button"
android:layout_marginTop="34dp"
android:layout_toLeftOf="#+id/forward_button"
android:text="#string/questionStr"
android:textSize="25sp" />
</RelativeLayout>
Android Manifest:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".QuizMeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
sorry for the pile of code, I am just frustrated and stuck
Try cleaning the project
Eclipse: Project->Clean

New activity in Android

I found myself in trouble with creating new activity, I get the unfortunately your app has stopped error message and as a good humanbeing I thought I'd share my misfortune with you:
I can't get my button to open new activity:
MainActivity.java:
package com.example.vogella.dev;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText text;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton =(RadioButton)findViewById(R.id.radio0);
RadioButtonfahrenheitButton=(RadioButton)findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this,getResources().getString(R.string.toast_a),
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(Strgin.valuof(ConvertFahrenheitToCelsius(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
} else {
text.setText(Strgin.valuof(ConvertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
private float ConvertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) +32;
}
public void scrollview(View v) {
Intent intent =newIntent(this,ScrollviewActivity.class);
startActivity(intent);
}
}
My activity_main.xml
<RelativeLayoutxmlns: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="#color/myColor" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/hint"
android:inputType="numberDecimal|numberSigned" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1">
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/celsius" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fahrenheit" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/radioGroup1"
android:onClick="onClick"
android:text="#string/calc" />
<Button
android:id="#+id/test_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="44dp"
android:layout_toRightOf="#+id/button1"
android:text="#string/test_button"
android:onClick="scrollview"/>
</RelativeLayout>
The AndroidManifest in portion
<activity
android:name="com.example.vogella.dev.Scrollview"
android:label="#string/title_activity_scrollview"
android:parentActivityName=com.example.vogella.dev.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.vogella.dev.MainActivity" />
</activity>
</application>
And the activity I'm trying to open
public class ScrollviewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollview);
TextView view = (TextView) findViewById(R.id.TextView02);
String s="";
for (int i=0; i < 500; i++) {
s += "vogella.com" ;
}
view.setText(s);
}
}
Intent intent =newIntent(this,ScrollviewActivity.class);
You forgot the space between new and Intent. However, you may have other problems as well. It would help to tell us the error thrown.
What is the error you are getting in the logcat. May be you have not mentioned your ScrollViewActivity in your Manifest file.Please check
Your manifest must contain the MainActivity:
<activity android:name="com.example.vogella.dev.MainActivity" />
1)use this in onCreate()
Button button = (Button) findViewById(R.id.button1);
you did initialize your EditText then why didnt you do it for your Button named 'button1'
2) even mention your ScrollViewActivity in android manifest
3) Intent i = new Intent(getApplicationContext,ScrollViewActivity.class);
startActivity(i);
Have you declare new Activity in Manifest? If not, add it like this:
<activity android:name="com.example.vogella.dev.ScrollviewActivity" />
How even you compile does IDE not implemented an error
Intent intent =newIntent(this,ScrollviewActivity.class); // Error is here
startActivity(intent);
It should be like this
Intent intent =new Intent(this,ScrollviewActivity.class);
startActivity(intent);
And also never forget to adding your every new activity to Manifest
<activity
android:name="com.activities.ActivitySettings"
android:icon="#drawable/icon_small"
android:label="#string/title_activity_activity_settings"
android:logo="#drawable/icon_small"
android:theme="#style/MyTheme" >
</activity>
Specifically for your question
You have two activity MainActivity and ScrollViewActivity
on manifest you mention this
<activity
android:name="com.example.vogella.dev.Scrollview"
android:label="#string/title_activity_scrollview"
Scrollview is not any of your activity but ScrollViewActivity is
Public void launchActivity(View view){
Intent intent = new Intent(this, youractivityname
Class);
startActivity(intent);

Categories

Resources