My app has crashed android eclipse? - java

I get an error when I run my android app on my android device. It says Unfortunately, GuessingGame has stopped. It was running before but ever since I added a EditText to the layout, it has been crashing. Here is my MainActivity.java:
package com.h2ogaming.numberguessinggame;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textOne = (TextView) findViewById(R.id.textView1);
Button pressMe = (Button) findViewById(R.id.button1);
pressMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String randText = "";
// TODO Auto-generated method stub
Random randGen = new Random();
int random = randGen.nextInt(5) + 1;
randText = Integer.toString(random);
textOne.setText(randText);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is my Activity_Main 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#2288ee">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="#string/guess_text"
android:textColor="#ffffff"
android:textSize="26sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="#string/text_view1"
android:textColor="#ffffff"
android:textSize="26sp" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="#string/btn_press" />
</RelativeLayout>
Please help me. Thanks!

Change your initialization for components.declare proper all components globally. then initialize it like below code:
public class MainActivity extends Activity {
TextView textOne;
Button pressMe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textOne = (TextView) findViewById(R.id.textView1);
pressMe = (Button) findViewById(R.id.button1);

Related

SeekBar not working while used in pop up window java android studio

Hi I've been trying to make a pop up window which presents a trackable seekbar as a part of a school project, both parts of the code work alone but when put together the seekbar progress is not tracked (you can move the seekbar but the number won't change), Help would be very appreciated!
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.activity_pop_up, null);
// create the popup window
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = 500;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window token
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonShowPopupWindowClick"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.368" />
</androidx.constraintlayout.widget.ConstraintLayout>
popUp.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
public class popUp extends AppCompatActivity {
private TextView textView;
private SeekBar seekBar;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
seekBar = (SeekBar) findViewById(R.id.seekBar);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressBar.setProgress(progress);
textView.setText("" + progress + "$");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
activity_pop_up.xml
<?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:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:id="#+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="confirm"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.06"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01999998" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="250dp"
android:layout_height="55dp"
android:progress="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.78"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01999998" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView3"
android:layout_width="154dp"
android:layout_height="45dp"
android:gravity="center"
android:text="amount:"
android:textSize="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.110000014" />
<TextView
android:id="#+id/textView"
android:layout_width="214dp"
android:layout_height="44dp"
android:text="0"
android:textSize="30dp"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.75"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.110000014" />
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/seekBar"
android:layout_alignParentStart="true"
android:layout_marginBottom="21dp" />
</LinearLayout>

EditText OnClick Exception

So I am making an app for an assignment.
Basically convert Miles to KM or KM to Miles.
I've now got the radio buttons working but when I add the onclick event, upon clicking the edittext fields I get an exception. :
java.lang.IllegalArgumentException:
Expected receiver of type a00891437.set3s.comp3975.convertunits.MainActivity,
but got android.support.v7.internal.widget.TintContextWrapper
Here is the code
package a00891437.set3s.comp3975.convertunits;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onRadioButtonClicked(final View view) {
View mText = findViewById(R.id.MilesText);
View kmText = findViewById(R.id.KilometersText);
if (view.getId() == R.id.KmToMButton) {
mText.setEnabled(false);
kmText.setEnabled(true);
}
if (view.getId() == R.id.MToKmButton) {
kmText.setEnabled(false);
mText.setEnabled(true);
}
}
public void onClickedText(final View view) {
Log.d("MainActivity", "Clicked");
}
}
The layout
<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=".MainActivity"
android:id="#+id/screen">
TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="110dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/KilometersText"
android:layout_above="#+id/button"
android:layout_marginBottom="134dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:enabled="false"
android:onClick="onClickedText"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/MilesText"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/KilometersText"
android:layout_alignStart="#+id/KilometersText"
android:enabled="false"
android:onClick="onClickedText"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Km"
android:id="#+id/Kilometers"
android:layout_alignTop="#+id/KilometersText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="M"
android:id="#+id/Miles"
android:layout_alignTop="#+id/MilesText"
android:layout_alignRight="#+id/Kilometers"
android:layout_alignEnd="#+id/Kilometers" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kilometers to Miles"
android:id="#+id/KmToMButton"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/MToKmButton"
android:layout_alignStart="#+id/MToKmButton"
android:checked="false"
android:onClick="onRadioButtonClicked"/>/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Miles to Kilometers"
android:id="#+id/MToKmButton"
android:layout_below="#+id/KmToMButton"
android:layout_alignLeft="#+id/Kilometers"
android:layout_alignStart="#+id/Kilometers"
android:checked="false"
android:onClick="onRadioButtonClicked"/>/>
</RadioGroup>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="59dp"
android:layout_marginStart="59dp" />
</RelativeLayout>
You are specifying the onClick event method through XML, and it looks like this does not work with support library AppCompatActivity.
You should just get the reference to the EditText and call setOnClickListener().
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText kilometers = (EditText) findViewById(R.id.KilometersText);
kilometers.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onClickedText(v);
}
});
// repeat this for the miles EditText
// you'll need to add an id for the radio group for this to work:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangedListener(new RadioGroup.OnCheckedChangedListener() {
// I leave the code inside here for you to do as an exercise
});
}

problems with activity layout when back button is pressed

I have this activity with EditViews and button. When the button is pressed I move on to a new activity. the problem is when the back button is pressed. The EditViews are not visible. What is expected is that the views and the text that was entered are visible. Also The buttons that appear are from the second activity and not the first.
<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.ammar.android.dailyjobsiteinspection2.MainActivity1Activity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="350dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/scrollView">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:text="New Text"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:visibility="invisible" />
<EditText
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/editText"
android:layout_gravity="center"
android:hint="Inspector ID"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:text="New Text"
android:id="#+id/textView2"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:visibility="invisible" />
<EditText
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/editText2"
android:layout_gravity="center"
android:hint="Site Name" />
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:text="New Text"
android:id="#+id/textView3"
android:layout_gravity="center_horizontal"
android:visibility="invisible"
android:layout_weight="1" />
<EditText
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/editText3"
android:layout_gravity="center"
android:hint="Site Address" />
<Button
android:layout_width="160dp"
android:layout_height="60dp"
android:text="Start"
android:id="#+id/button3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:onClick="start"
android:layout_margin="5dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here is the Java code:
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity1Activity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity1, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void start(View view){
EditText editText1 = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
EditText editText3 = (EditText) findViewById(R.id.editText3);
if(!allFieldsEntered(editText1, editText2, editText3)){
com.ammar.android.dailyjobsiteinspection2.Message.message(this, "All fields must be filled before starting an Audit ");
}else{
Intent intent = new Intent(this, MainActivity11Activity.class);
Bundle bundle = getDataBundle(editText1, editText2, editText3);
intent.putExtras(bundle);
setContentView(R.layout.activity_main_activity11);
startActivity(intent);
}
}
private Bundle getDataBundle(EditText editText1, EditText editText2, EditText editText3) {
Bundle bundle = new Bundle();
bundle.putString(MainActivity.INSPECTORID, editText1.getText().toString());
bundle.putString(MainActivity.SITENAME, editText2.getText().toString());
bundle.putString(MainActivity.SITEADDRESS, editText3.getText().toString());
return bundle;
}
private boolean allFieldsEntered(EditText editText1, EditText editText2, EditText editText3) {
return !(editText1.getText().toString().matches("")
|| editText2.getText().toString().matches("")
|| editText3.getText().toString().matches(""));
}
}
Here is the second activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.ammar.android.dailyjobsiteinspection2.MainActivity11Activity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/scrollView"
android:layout_gravity="center_horizontal"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"></LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/layout2">
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView4"
android:layout_weight="1"
android:visibility="invisible" />
<Button
android:layout_width="160dp"
android:layout_height="fill_parent"
android:text="Save"
android:id="#+id/button4"
android:onClick="save" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView5"
android:layout_weight="1"
android:visibility="invisible"/>
<Button
android:layout_width="160dp"
android:layout_height="fill_parent"
android:text="Submit"
android:id="#+id/button5"
android:onClick="submit" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView6"
android:layout_weight="1"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
and the Java code:
import android.app.ActionBar;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity11Activity extends ActionBarActivity {
private ArrayList<RadioGroup> radioGroupArrayList;
private String inspectorId;
private String siteName;
private String siteAddress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity11);
setFields();
setUI();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity11, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setFields() {
Intent intentBundle = getIntent(); // get intent
Bundle bundle = intentBundle.getExtras(); // get bundle from intent
inspectorId = bundle.getString(MainActivity.INSPECTORID);
siteName = bundle.getString(MainActivity.SITENAME);
siteAddress = bundle.getString(MainActivity.SITEADDRESS);
}
private void setUI() {
TextView textView;
radioGroupArrayList = new ArrayList<RadioGroup>();
LinearLayout ll = (LinearLayout) this.findViewById(R.id.layout);
int count = 0;
for(int i = 0; i<6; i++){
int id = getResources().getIdentifier("PreStart"+ i, "array" , getPackageName());
String [] questions = getResources().getStringArray(id);
textView = new TextView(this);
textView.setText(questions[0]);
textView.setTextSize(24);
textView.setTypeface(null, Typeface.BOLD);
ll.addView(textView);
TextView textView1;
TextView textView2;
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
for (int j = 1; j< questions.length; j++){
textView1 = new TextView(this);
textView1.setText(questions[j]);
textView1.setTextSize(18);
radioGroup = new RadioGroup(this);
radioGroupArrayList.add(radioGroup);
radioButton1 = new RadioButton(this);
radioButton2 = new RadioButton(this);
radioButton3 = new RadioButton(this);
radioButton1.setText("Yes");
radioButton2.setText("No");
radioButton3.setText("NA");
radioButton1.setId(count);
radioButton2.setId(count+1);
radioButton3.setId(count+2);
count = count + 3;
radioGroup.addView(radioButton1);
radioGroup.addView(radioButton2);
radioGroup.addView(radioButton3);
radioButton1.setPadding(20, 20, 100, 20);
radioButton2.setPadding(20, 20, 100, 20);
radioButton3.setPadding(20, 20, 100, 20);
radioGroup.setOrientation(LinearLayout.HORIZONTAL);
textView2 = new TextView(this);
textView2.setVisibility(View.INVISIBLE);
ll.addView(textView1);
ll.addView(radioGroup);
ll.addView(textView2);
}
}
EditText editText = new EditText(this);
editText.setId(R.id.editText11);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setHint("Enter Comments Here");
ll.addView(editText);
}
public void save(View view){
AnswerListCreator answerCreator = new AnswerListCreator(radioGroupArrayList);
String[] answerList = answerCreator.getAnswerList();
String commentLog = ((EditText) findViewById(R.id.editText11)).getText().toString();
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Site site = new Site(inspectorId, siteName, siteAddress, answerList, commentLog, 0);
dbHelper.insertSite(db, site);
// go back to main
Intent intent = new Intent(this, MainActivity.class);
setContentView(R.layout.activity_main);
startActivity(intent);
db.close();
finish();
}
public void submit(View view) {
AnswerListCreator answerCreator = new AnswerListCreator(radioGroupArrayList);
if (!answerCreator.isComplete()) {
com.ammar.android.dailyjobsiteinspection2.Message.message(this, "All fields must be checked");
} else {
answerCreator = new AnswerListCreator(radioGroupArrayList);
String[] answerList = answerCreator.getAnswerList();
String commentLog = ((EditText) findViewById(R.id.editText11)).getText().toString();
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Site site = new Site(inspectorId, siteName, siteAddress, answerList, commentLog, 1);
dbHelper.insertSite(db, site);
// go back to main
Intent intent = new Intent(this, MainActivity.class);
setContentView(R.layout.activity_main);
startActivity(intent);
db.close();
finish();
}
}
}
You are changing first Activity's View before calling StartActivity.
Remove this in MainActivity1Activity:
setContentView(R.layout.activity_main_activity11);
Furthermore, if you want to keep view state in MainActivity1Activity don't start it again. Just call finish() in second activity.

unfortunately,myapp has stopped working [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 8 years ago.
Here is my code and something i am in need is to use EditText as my output,i have no errors but after the main activity is loaded it say unfortunately myapp has stopped working.
Here is my MainActivity.java code::
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener{
Button b1,b2;
EditText t1,t2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
t1 = (EditText) findViewById(R.id.editText2);
}
public void onClick(View v) {
float b;
if(v.getId()==R.id.button1){
float a = Float.valueOf(t1.getText().toString());
b=a*1024;
t2.setText(" "+b,TextView.BufferType.EDITABLE);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
}
xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.techbolt.byteconverter.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="#string/bit" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:text="#string/button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/bytes" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/textView2"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/bytes" />
</RelativeLayout>
You forgot to initialize your t2 variable. Please add below line:
t1 = (EditText) findViewById(R.id.editText1);
t2 = (EditText) findViewById(R.id.editText2);
Try to change
public void onClick(View v) {
float b;
if(v.getId()==R.id.button1){
float a = Float.valueOf(t1.getText().toString());
b=a*1024;
t2.setText(" "+b,TextView.BufferType.EDITABLE);
}
}
Via
public void onClick(View v) {
double b;
if(v.getId()==R.id.button1){
double e1 = Double.parseDouble(t1.getText().toString());
b=a*1024;
t2.setText(" "+String.valueOf(b),TextView.BufferType.EDITABLE);
}
}
and add
t1 = (EditText) findViewById(R.id.editText1);
t2 = (EditText) findViewById(R.id.editText2);

Simple program force closing

I'm making a program for Android using Eclipse. Here is my code for MainAcitvity.java:
package com.example.changeme;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button set = (Button)findViewById(R.id.set);
TextView out = (TextView)findViewById(R.id.out);
EditText in = (EditText)findViewById(R.id.input);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
set.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setcontent();
}
});
}
public void setcontent()
{
String con = in.getText().toString();
out.setText(con);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return false;
}
}
Layout for main activity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/set"
android:layout_alignParentBottom="true"
android:layout_marginBottom="37dp"
android:text="#string/Change"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/set"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/input"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="#string/Set" />
<EditText
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:ems="10"
android:inputType="textPersonName" />
It crashes when I run the program on my N4.
You need to inflate your layout before retrieve your elements, otherwise findViewById will return null and hence the line set.setOnClickListener(/**/); will throw a NullPointerException which make your app crashing.
Button set;
TextView out;
EditText in;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //<-- the layout is inflated, i.e it's rendered
/******************************************************/
//Now you can retrieve your elements from this layout
set = (Button)findViewById(R.id.set);
out = (TextView)findViewById(R.id.out);
in = (EditText)findViewById(R.id.input);
/*****************************************************/
set.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setcontent();
}
});
}

Categories