Have problem that when I press button application crashes.
Crash text
package com.leftas.vidurkioskaiciavimas;
24367-24367/com.leftas.vidurkioskaiciavimas E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3704)
at android.view.View.performClick(View.java:4232)
at android.view.View$PerformClick.run(View.java:17298)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3699)
... 11 more
Caused by: java.lang.NullPointerException
at com.leftas.vidurkioskaiciavimas.vidurkioskaiciuokle.buttonEventHandler(vidurkioskaiciuokle.java:85)
... 14 more
I thinked that text is null because all time crashes on
"mUserOutput.setText(""); or "mUserInput.setText(mUserInput.getText() + "10");"
So I added
if (mUserOutput == null)
Log.e("output", null);
if (mUserInput == null)
Log.e("input", null);
But Nothing outputted...
So here is my main activity.
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class vidurkioskaiciuokle extends Activity {
int c;
TextView mUserOutput,mUserInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vidurkio_skaiciuokle);
TextView mUserOutput = (TextView) findViewById(R.id.textView2);
TextView mUserInput = (TextView) findViewById(R.id.textView);
if (mUserOutput == null)
Log.e("output", null);
if (mUserInput == null)
Log.e("input", null);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.vidurkioskaiciuokle, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.exit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void buttonEventHandler(View v) {
switch(v.getId()) {
case R.id.button1 :
mUserInput.setText(mUserInput.getText() + "1");
c = +1;
break;
case R.id.button2 :
mUserInput.setText(mUserInput.getText() + "2");
c = +2;
break;
case R.id.button3 :
mUserInput.setText(mUserInput.getText() + "3");
c = +3;
break;
case R.id.button4 :
mUserInput.setText(mUserInput.getText() + "4");
c = +4;
break;
case R.id.button5 :
mUserInput.setText(mUserInput.getText() + "5");
c = +5;
break;
case R.id.button6 :
mUserInput.setText(mUserInput.getText() + "6");
c = +6;
break;
case R.id.button7 :
mUserInput.setText(mUserInput.getText() + "7");
c = +7;
break;
case R.id.button8 :
mUserInput.setText(mUserInput.getText() + "8");
c = +8;
break;
case R.id.button9 :
mUserInput.setText(mUserInput.getText() + "9");
c = +9;
break;
case R.id.button10 :
mUserInput.setText(mUserInput.getText() + "10");
c = +10;
break;
case R.id.buttonclear :
mUserOutput.setText("");
mUserInput.setText("");
c = 0;
double average = 0;
break;
}
}
public void computeAverage(int [] scores)
{
double average = 0;
for (int c : scores) {
average += c;
}
average /= scores.length;
mUserOutput.setText(Double.toString(average));
}
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: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=".vidurkioskaiciuokle">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="buttonEventHandler" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button2"
android:layout_alignTop="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:onClick="buttonEventHandler" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/button3"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:onClick="buttonEventHandler" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button4"
android:layout_alignTop="#+id/button3"
android:layout_toRightOf="#+id/button3"
android:onClick="buttonEventHandler" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/button5"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:onClick="buttonEventHandler" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/button6"
android:layout_alignTop="#+id/button5"
android:layout_toRightOf="#+id/button5"
android:onClick="buttonEventHandler" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button7"
android:layout_above="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:onClick="buttonEventHandler" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button8"
android:layout_alignTop="#+id/button9"
android:layout_toLeftOf="#+id/button9"
android:onClick="buttonEventHandler" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button9"
android:onClick="buttonEventHandler"
android:layout_alignTop="#+id/button7"
android:layout_toRightOf="#+id/button1" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:id="#+id/button10"
android:layout_above="#+id/button4"
android:layout_toLeftOf="#+id/button5"
android:onClick="buttonEventHandler" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/button8"
android:layout_above="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false"
android:layout_alignWithParentIfMissing="false"
android:layout_alignLeft="#+id/button8"
android:layout_alignRight="#+id/textView"
android:layout_above="#+id/button7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="#+id/buttonclear"
android:onClick="buttonEventHandler"
android:layout_above="#+id/button5"
android:layout_toRightOf="#+id/button4"
android:layout_alignRight="#+id/textView2" />
I will be thankful if you could me help :)
If something need ask for it I will edit my question. :)
Thanks :)
Also If something wrong please say where and how to change it ;)
try this
mUserOutput = (TextView) findViewById(R.id.textView2);
mUserInput = (TextView) findViewById(R.id.textView);
You have already Declared textview's as class member. So initialize them as above.
This
TextView mUserOutput = (TextView) findViewById(R.id.textView2);
// makes mUserOutput local
findviewbyid and mUserOutput.setOnClickListener(this); required .
also implement onLCicklistener like:
public class MainActivity extends Activity implements OnClickListener{
Related
I am building a math app that has timed multiple choice questions for addition, subtraction, multiplication, and division. The code is pretty simple and is the same for each activity. However, My subtraction activity is very buggy and always crashes at random times. Sometimes it will crash after pressing GO, sometimes it crashes after choosing the first answer, sometimes I can get to 3 questions then it crashes. I'm confused because it does not happen with addition or multiplication. I'm wondering if it has something to do with memory?
Anyways, here is my code and error. Thanks in advance!
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.*;
public class subActivity extends AppCompatActivity {
Button button0;
Button button1;
Button button2;
Button button3;
TextView sub;
Button startButton;
TextView resultTextView;
TextView pointsTextView;
ArrayList<Integer> answers = new ArrayList<Integer>();
int locationOfCorrectAnswer;
int score = 0;
int numberOfQuestions = 0;
TextView timerTextView;
Button playAgainButton;
RelativeLayout gameRelativeLayout;
GridLayout buttonLayout;
float percent;
public void playAgain(View view){
score = 0;
numberOfQuestions = 0;
timerTextView.setText("30s");
pointsTextView.setText("0/0");
resultTextView.setText("");
playAgainButton.setVisibility(View.INVISIBLE);
generateQuestion();
new CountDownTimer(30100, 1000) {
#Override
public void onTick(long millisUntilFinished) {
buttonLayout.setVisibility(View.VISIBLE);
sub.setVisibility(View.VISIBLE);
timerTextView.setText(String.valueOf(millisUntilFinished/1000) + "s");
}
#Override
public void onFinish() {
playAgainButton.setVisibility(View.VISIBLE);
timerTextView.setText("0s");
resultTextView.setText("Your score:" + percent + "%\n Questions:" + Integer.toString(score) + "/" + numberOfQuestions);
buttonLayout.setVisibility(View.INVISIBLE);
sub.setVisibility(View.INVISIBLE);
}
}.start();
}
public void generateQuestion() {
Random rand = new Random();
int a = rand.nextInt(21);
int b = rand.nextInt(21);
int incorrectAnswer;
sub.setText(a + " - " + b);
locationOfCorrectAnswer = rand.nextInt(4);
answers.clear();
for (int i=0; i<4;i++){
if(i == locationOfCorrectAnswer){
answers.add(a-b);
}
else{
incorrectAnswer = rand.nextInt((a-b));
while (incorrectAnswer == a-b) {
incorrectAnswer = rand.nextInt((a-b));
}
answers.add(incorrectAnswer);
}
}
button0.setText(Integer.toString(answers.get(0)));
button1.setText(Integer.toString(answers.get(1)));
button2.setText(Integer.toString(answers.get(2)));
button3.setText(Integer.toString(answers.get(3)));
}
public void start(View view) {
startButton.setVisibility(View.INVISIBLE);
gameRelativeLayout.setVisibility(RelativeLayout.VISIBLE);
playAgain(findViewById(R.id.playAgain));
}
public void chooseAnswer(View view) {
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))) {
score++;
resultTextView.bringToFront();
resultTextView.setText("Correct!");
//resultTextView.setBackgroundColor(-16711936);
} else {
resultTextView.bringToFront();
resultTextView.setText("Incorrect!");
//resultTextView.setBackgroundColor(-65536);
}
numberOfQuestions++;
pointsTextView.setText(Integer.toString(score) + "/" + numberOfQuestions);
percent = Math.round((score/(float)numberOfQuestions)*100.0); //get the percentage recieved
generateQuestion();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
startButton = (Button) findViewById(R.id.goButton);
sub = (TextView) findViewById(R.id.subTextView);
resultTextView = (TextView) findViewById(R.id.resultTextView);
pointsTextView = (TextView) findViewById(R.id.pointsTextView);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
timerTextView = (TextView) findViewById(R.id.timerTextView);
playAgainButton = (Button) findViewById(R.id.playAgain);
gameRelativeLayout = (RelativeLayout) findViewById(R.id.gameRelativeLayout);
buttonLayout = (GridLayout) findViewById(R.id.buttonLayout);
}
}
This error shows up when it crashes:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jimmy.mathtime, PID: 14678
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5224)
at android.view.View$PerformClick.run(View.java:21356)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5224)
at android.view.View$PerformClick.run(View.java:21356)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.IllegalArgumentException: n <= 0: -1
at java.util.Random.nextInt(Random.java:182)
at com.example.jimmy.mathtime.subActivity.generateQuestion(subActivity.java:84)
at com.example.jimmy.mathtime.subActivity.chooseAnswer(subActivity.java:124)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5224)
at android.view.View$PerformClick.run(View.java:21356)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.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.example.jimmy.mathtime.subActivity">
<RelativeLayout
android:id="#+id/gameRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<GridLayout
android:id="#+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/subTextView">
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:background="#android:color/holo_blue_bright"
android:onClick="chooseAnswer"
android:tag="0"
android:text="#string/_3"
android:textSize="80sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:background="#color/colorAccent"
android:onClick="chooseAnswer"
android:tag="1"
android:text="#string/_3"
android:textSize="80sp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:background="#android:color/holo_green_light"
android:onClick="chooseAnswer"
android:tag="2"
android:text="#string/_3"
android:textSize="80sp" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:background="#android:color/holo_purple"
android:onClick="chooseAnswer"
android:tag="3"
android:text="#string/_3"
android:textSize="80sp" />
</GridLayout>
<TextView
android:id="#+id/subTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/timerTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:padding="10dp"
android:text="#string/_3_x_3"
android:textColor="#android:color/black"
android:textSize="50sp"
android:textStyle="bold" />
<TextView
android:id="#+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="15dp"
android:layout_marginTop="14dp"
android:padding="10dp"
android:text="#string/_30s"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="#+id/playAgain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/buttonLayout"
android:layout_centerHorizontal="true"
android:layout_marginBottom="65dp"
android:background="#color/colorPrimary"
android:onClick="playAgain"
android:text="Play_again"
android:textSize="30sp"
android:visibility="invisible" />
<TextView
android:id="#+id/pointsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/timerTextView"
android:layout_marginEnd="16dp"
android:padding="10dp"
android:text="#string/_0_0"
android:textSize="30sp"
android:textStyle="bold"
tools:ignore="RelativeOverlap" />
<TextView
android:id="#+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="20dp"
android:text="#string/correct"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<Button
android:id="#+id/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:backgroundTint="#android:color/holo_blue_bright"
android:onClick="start"
android:padding="40dp"
android:text="#string/go"
android:textSize="80sp"
android:visibility="visible" />
</android.widget.RelativeLayout>
Your exception states:
Caused by: java.lang.IllegalArgumentException: n <= 0: -1
at java.util.Random.nextInt(Random.java:182)
at com.example.jimmy.mathtime.subActivity.generateQuestion(subActivity.java:84)
Basically you pass a negative value to rand.nextInt(max) method.
In your code you do the following operation:
incorrectAnswer = rand.nextInt((a-b));
If b is greater than a, you pass a negative number to nextInt function. This causes your problem. It is not allowed to do this. Check if (a-b)>0!
I'm making a quiz app and I'm using one EditText, and when I don't type anything in it and press SubmitButton I get this error:
FATAL EXCEPTION: main
Process: com.example.andriod.quiz, PID: 12960
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5264)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5264)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:284)
at android.widget.TextView.setText(TextView.java:4176)
at com.example.andriod.quiz.MainActivity.submitAnswer(MainActivity.java:65)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5264)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
Here is my java code:
package com.example.andriod.quiz;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
/**
* This app displays Millionaire Quiz
*/
public class MainActivity extends AppCompatActivity {
private String name;
private int correctAnswers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the Ok button is clicked. it's displays personalized greetings.
*
* #param view
*/
public void greetings(View view) {
EditText nameField = (EditText) findViewById(R.id.customer_name);
name = nameField.getText().toString();
TextView greetings = (TextView) findViewById(R.id.greetings);
greetings.setText("Hello " + name + ". Scroll down if you are ready.");
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
LinearLayout questionsLayout = (LinearLayout) findViewById(R.id.questions_layout);
questionsLayout.setVisibility(View.VISIBLE);
}
/**
* Checks correct answers, and displays a toast with congratulations and amount of correctly answered questions.
*
* #param view
*/
public void submitAnswer(View view) {
correctAnswers = 0;
RadioButton firstQuestion = (RadioButton) findViewById(R.id.first_correct);
if (firstQuestion.isChecked()) {
correctAnswers++;
}
EditText secondQuestion = (EditText) findViewById(R.id.second_question);
int answer = Integer.parseInt(secondQuestion.getText().toString());
if (answer == 27) {
correctAnswers++;
}
CheckBox checkBoxA = (CheckBox) findViewById(R.id.answer_a);
CheckBox checkBoxB = (CheckBox) findViewById(R.id.answer_b);
CheckBox checkBoxC = (CheckBox) findViewById(R.id.answer_c);
CheckBox checkBoxD = (CheckBox) findViewById(R.id.answer_d);
if (!checkBoxA.isChecked() && checkBoxB.isChecked() && checkBoxC.isChecked() && !checkBoxD.isChecked()) {
correctAnswers++;
}
RadioButton fourthQuestion = (RadioButton) findViewById(R.id.fourth_correct);
if (fourthQuestion.isChecked()) {
correctAnswers++;
}
RadioButton fifthQuestion = (RadioButton) findViewById(R.id.fifth_correct);
if (fifthQuestion.isChecked()) {
correctAnswers++;
}
String correctlyAnswered;
switch (correctAnswers) {
case 5:
correctlyAnswered = "Congratulation " + name + " you answer correctly to every question! You won 1 million! ";
break;
case 4:
correctlyAnswered = "Congratulation " + name + " you answer correctly to 4 questions! You won 750 thousands! You should try one more time!";
break;
case 3:
correctlyAnswered = "Congratulation " + name + " you answer correctly to 3 questions! You won 500 thousands! You should try one more time!";
break;
case 2:
correctlyAnswered = "Congratulation " + name + " you answer correctly to 3 questions! You won 250 thousands! You should try one more time!";
break;
case 1:
correctlyAnswered = "Congratulation " + name + " you answer correctly to 1 question! You won 100 thousands! You should try one more time!";
break;
default:
correctlyAnswered = "You didn't answer correctly to any question :( \nYou should try one more time!";
}
Toast toast = Toast.makeText(this, correctlyAnswered, Toast.LENGTH_LONG);
toast.show();
}
}
And here is xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/second_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6158AC"
android:orientation="vertical"
tools:context="com.example.andriod.quiz.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:text="Welcome in Millionaire Game! \nAre you ready to play for a million?"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/customer_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Name"
android:inputType="textCapWords" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="greetings"
android:text="Ok"
android:textAllCaps="true" />
</LinearLayout>
<TextView
android:id="#+id/greetings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:textSize="24sp" />
<LinearLayout
android:id="#+id/questions_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First question: \nHow many parts has Harry Potter series?" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/first_correct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seven" /><!--correct-->
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eight" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Six" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second question: \nOwen thinks of a number, adds 13, and then divides the result by 5. The answer is 8. Find the number Owen thinks of." />
<EditText
android:id="#+id/second_question"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:hint="Write here your answer." />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third question: \nWhat is a value of absolute zero?" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="#+id/answer_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-189°C" />
<CheckBox
android:id="#+id/answer_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-271,15°C" /><!--correct-->
<CheckBox
android:id="#+id/answer_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0K" /><!--correct-->
<CheckBox
android:id="#+id/answer_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100K" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fourth question: \nWhat is the main component in glass?" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/fourth_correct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sand" /> <!--correct-->
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Iron" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coal" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Water" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fifth question: \nWhich is the largest species of the tiger?" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chinese tiger" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bengali tiger" />
<RadioButton
android:id="#+id/fifth_correct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Siberian tiger" /><!--correct-->
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Indo-Chinese tiger" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submitAnswer"
android:text="Submit answers"
android:textAllCaps="true" />
</LinearLayout>
</LinearLayout>
Does anyone knows why this error occurs?
I'm sorry for every mistake I made, this is my first question here ;)
It fails here:
int answer = Integer.parseInt(secondQuestion.getText().toString());
You must have a not empty field here, and also it should be a number.
You can do simply check:
int answer = 0;
EditText secondQuestion = (EditText) findViewById(R.id.second_question);
if(secondQuestion.getText().toString().equals("")) {
//Handle invalid input
} else {
answer = Integer.parseInt(secondQuestion.getText().toString());
}
Just implement some validation of invalid input, and this can be handled easily.
I'm doing a simple calculator in android studio, my code looks like this:
MainActivity.java:
package com.example.ja.calculator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
String total="";
double v1, v2;
String sign="";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void onClick(View v){
Button button = (Button)v;
String str=button.getText().toString();
total+=str;
EditText edit=(EditText)findViewById(R.id.editText);
edit.setText(total);
}
public void OnAdd(View v){
v1 = Double.parseDouble(total);
total = "";
Button button = (Button)v;
String str = button.getText().toString();
sign = str;
EditText edit = (EditText)findViewById(R.id.editText);
edit.setText("");
}
public void OnCalculate(View v){
EditText edit = (EditText)findViewById(R.id.editText);
String str2 = edit.getText().toString();
v2 = Double.parseDouble(str2);
double grand_total = 0;
if(sign.equals("+")){
grand_total = v1 + v2;
}
else if(sign.equals("-")){
grand_total = v1 - v2;
}
else if(sign.equals("x")){
grand_total = v1 * v2;
}
else if(sign.equals(":")){
grand_total = v1 / v2;
}
edit.setText(grand_total+"");
}
public void OnClear(View v){
EditText edit = (EditText)findViewById(R.id.editText);
edit.setText("");
total = "";
}
#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);
}
}
content main:
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.ja.calculator.MainActivity"
tools:showIn="#layout/activity_main">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/button3"
android:layout_alignTop="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:layout_toEndOf="#+id/button2"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button4"
android:layout_above="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/button5"
android:layout_above="#+id/button2"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/button6"
android:layout_above="#+id/button3"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button7"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button8"
android:layout_alignTop="#+id/button7"
android:layout_toRightOf="#+id/button4"
android:layout_toEndOf="#+id/button4"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button9"
android:layout_alignTop="#+id/button8"
android:layout_toRightOf="#+id/button5"
android:layout_toEndOf="#+id/button5"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/button0"
android:layout_below="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="#+id/buttonDot"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/buttonEQ"
android:layout_below="#+id/button3"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3"
android:height="80dp"
android:onClick="OnCalculate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/buttonAdd"
android:height="64dp"
android:layout_alignBottom="#+id/buttonEQ"
android:layout_alignLeft="#+id/buttonSub"
android:layout_alignStart="#+id/buttonSub"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/buttonSub"
android:height="64dp"
android:layout_above="#+id/buttonAdd"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:id="#+id/buttonMul"
android:height="64dp"
android:layout_above="#+id/buttonSub"
android:layout_alignLeft="#+id/buttonSub"
android:layout_alignStart="#+id/buttonSub"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:id="#+id/buttonDiv"
android:layout_above="#+id/buttonMul"
android:layout_alignLeft="#+id/buttonMul"
android:layout_alignStart="#+id/buttonMul"
android:height="64dp"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CE"
android:id="#+id/buttonCE"
android:height="64dp"
android:layout_above="#+id/buttonDiv"
android:layout_toRightOf="#+id/button9"
android:layout_toEndOf="#+id/button9"
android:onClick="OnClear" />
The problem is, when I click some number and after that I click the operation (+,-,/,*) the number clicked before disappears, how to fix it, so I can see full operation, for example 5*5 before clicking "=" and after that changing just for result?
public void OnAdd(View v) {
v1 = Double.parseDouble(total);
total = "";
Button button = (Button) v;
String str = button.getText().toString();
sign = str;
EditText edit = (EditText) findViewById(R.id.editText); ** edit.setText("");
}
The reason for number clicked before gets cleared is that you set text as "". (see **)
You should do the following changes to your code if your calculator to work in the correct way.
public void OnAdd(View v) {
v1 = Double.parseDouble(total);
Button button = (Button) v;
String str = button.getText().toString();
sign = str;
total += str;
EditText edit = (EditText) findViewById(R.id.editText);
edit.setText(total);
}
Now the sign will be appended to the total string. And it will be written back to the editText.
Simple calc, For every button I must define a value, can i get it direct from button ?
Its a simple app, but when i want to test it, I get error "app stopped". I'm using one listener for all buttons, I think it causes an error.
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;
public class MainActivity extends Activity implements OnClickListener {
Button buttOne, buttTwo, buttThree, buttFour, buttFive, buttSix, buttSeven, buttEight, buttNine, buttZero;
Button buttSum, buttSubstr;
Double result, buffer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttOne = (Button) findViewById(R.id.one);
buttTwo = (Button) findViewById(R.id.two);
buttThree = (Button) findViewById(R.id.three);
buttFour = (Button) findViewById(R.id.four);
buttFive = (Button) findViewById(R.id.five);
buttSix = (Button) findViewById(R.id.six);
buttSeven = (Button) findViewById(R.id.seven);
buttEight = (Button) findViewById(R.id.eight);
buttNine = (Button) findViewById(R.id.nine);
buttZero = (Button) findViewById(R.id.zero);
buttSum = (Button) findViewById(R.id.sum);
buttSubstr = (Button) findViewById(R.id.substr);
buttOne.setOnClickListener(this);
buttTwo.setOnClickListener(this);
buttThree.setOnClickListener(this);
buttFour.setOnClickListener(this);
buttFive.setOnClickListener(this);
buttSix.setOnClickListener(this);
buttSeven.setOnClickListener(this);
buttEight.setOnClickListener(this);
buttNine.setOnClickListener(this);
buttZero.setOnClickListener(this);
buttSum.setOnClickListener(this);
buttSubstr.setOnClickListener(this);
result = 0.0;
}
/*
* #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 void onClick(View v) {
switch (v.getId()) {
case R.id.one:
buffer = 1.0;
break;
case R.id.two:
buffer = 2.0;
break;
case R.id.three:
buffer = 3.0;
break;
case R.id.four:
buffer = 4.0;
break;
case R.id.five:
buffer = 5.0;
break;
case R.id.six:
buffer = 6.0;
break;
case R.id.seven:
buffer = 7.0;
break;
case R.id.eight:
buffer = 8.0;
break;
case R.id.nine:
buffer = 9.0;
break;
case R.id.zero:
buffer = 0.0;
break;
case R.id.substr:
result = -buffer;
break;
case R.id.sum:
result = +buffer;
}
}
}
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:background="#style/AppTheme"
android:gravity="center_horizontal"
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" >
<Button
android:id="#+id/seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/seven" />
<Button
android:id="#+id/eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/seven"
android:layout_alignBottom="#+id/seven"
android:layout_toRightOf="#+id/seven"
android:text="#string/eight" />
<Button
android:id="#+id/nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/eight"
android:layout_alignBottom="#+id/eight"
android:layout_toRightOf="#+id/eight"
android:text="#string/nine" />
<Button
android:id="#+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/seven"
android:layout_below="#+id/seven"
android:text="#string/four" />
<Button
android:id="#+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/four"
android:layout_alignBottom="#+id/four"
android:layout_toRightOf="#+id/four"
android:text="#string/five" />
<Button
android:id="#+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nine"
android:layout_below="#+id/nine"
android:text="#string/six" />
<Button
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/four"
android:layout_below="#+id/four"
android:text="#string/one" />
<Button
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/five"
android:layout_below="#+id/five"
android:text="#string/two" />
<Button
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/six"
android:layout_below="#+id/six"
android:text="#string/three" />
<Button
android:id="#+id/zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/one"
android:layout_toLeftOf="#+id/three"
android:text="#string/zero" />
<Button
android:id="#+id/division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/nine"
android:text="#string/division" />
<Button
android:id="#+id/multiple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/six"
android:layout_alignBottom="#+id/six"
android:layout_toRightOf="#+id/six"
android:text="#string/multiple" />
<Button
android:id="#+id/substr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/six"
android:layout_toRightOf="#+id/six"
android:text="#string/subst" />
<Button
android:id="#+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/three"
android:layout_toRightOf="#+id/three"
android:text="#string/sum" />
<Button
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/sum"
android:layout_alignBottom="#+id/sum"
android:layout_toLeftOf="#+id/sum"
android:text="#string/result" />
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/one"
android:layout_below="#+id/one"
android:text="#string/clear" />
<EditText
android:id="#+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/eight"
android:layout_alignRight="#+id/division"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/eight"
android:layout_alignRight="#+id/division"
android:ems="10"
android:inputType="numberDecimal" />
console:
[2014-01-16 15:42:41 - ddmlib] An established connection was aborted by the software installed on the host computer java.io.IOException: An established connection was aborted by the software installed on the host computer
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(Unknown Source)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.write(Unknown Source)
at sun.nio.ch.SocketChannelImpl.write(Unknown Source)
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:642)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:488)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:835)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:803)
at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:763)
at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:652)
at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:44)
at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:580)
You need to add the onClickListeners.
buttOne.setOnClickListener(this);
and so on ... :)
Maybe this is for you. Use the android:tag like proposed in OcuS' answer.
my first question so I hope I'm doing this right.
I have added tablet layouts to my Android project and now I have a few crash reports on various devices. It works 100% just fine for me, however, testers with tablets get force closed at runtime.
The app is a simple dashboard allowing users to apply themes to third party launchers, Much like the Stark and Flatro icon packs.
Here's the errors:
java.lang.NoClassDefFoundError: com.arandompackage.jive.Fragment1$1
at com.arandompackage.jive.Fragment1.onActivityCreated(Fragment1.java:57)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
at android.view.View.measure(View.java:12911)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:828)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
at android.view.View.measure(View.java:12911)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
at android.view.View.measure(View.java:12911)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:828)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
at android.view.View.measure(View.java:12911)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2097)
at android.view.View.measure(View.java:12911)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
And
java.lang.RuntimeException: Unable to instantiate receiver com.arandompackage.jive.AnalogClockWidget: java.lang.ClassNotFoundException: com.arandompackage.jive.AnalogClockWidget
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2261)
at android.app.ActivityThread.access$1600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4946)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.arandompackage.jive.AnalogClockWidget
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2256)
... 10 more
I have done everything that all of these posts suggest, to no avail.
Here's the fragment1 java
package com.arandompackage.jive;
import com.arandompackage.jive.R;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
public class Fragment1 extends Fragment{
static final String ACTION_APPLY_ICON_THEME = "com.teslacoilsw.launcher.APPLY_ICON_THEME";
public static final String ACTION_MYTHEME = "com.gau.go.launcherex.MyThemes.mythemeaction";
public static final String ACTION_SET_THEME = "com.anddoes.launcher.SET_THEME";
static final String EXTRA_ICON_THEME_PACKAGE = "com.teslacoilsw.launcher.extra.ICON_THEME_PACKAGE";
static final String EXTRA_ICON_THEME_TYPE = "com.teslacoilsw.launcher.extra.ICON_THEME_TYPE";
public static final String EXTRA_PACKAGE_NAME = "com.anddoes.launcher.THEME_PACKAGE_NAME";
ImageButton button;
Button button2;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_1, null);
}
private boolean isAppInstalled(String uri) {
PackageManager pm = getActivity().getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
#Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
button = (ImageButton) getActivity().findViewById(R.id.apextheme);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isAppInstalled("com.anddoes.launcher")) {
Intent intent3 = new Intent("com.anddoes.launcher.SET_THEME");
intent3.putExtra("com.anddoes.launcher.THEME_PACKAGE_NAME", getActivity().getPackageName());
startActivity(intent3);
Crouton.makeText(getActivity(), "TAP APPLY TO FINALIZE", Style.INFO, (ViewGroup) getView()).show();
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(R.string.apexnotfound);
alert.setMessage(R.string.apexmarket);
alert.setIcon(R.drawable.apex_1);
alert.setPositiveButton((R.string.yes),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("market://details?id=com.anddoes.launcher"));
startActivity(intent);
}
});
alert.setNegativeButton((R.string.no),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
return;
}
});
alert.show();
}
}
});
And the xml for fragment_1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#f9f9f9"
android:paddingBottom="15dp" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" android:scrollbars="none"
android:overScrollMode="never">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="vertical" >
<!-- Action -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="40dp" >
<ImageButton
android:id="#+id/actiontheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#drawable/btn_default_holo_dark"
android:paddingBottom="15dp"
android:src="#drawable/alp_1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/actiontheme"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/action"
android:textSize="24dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/actiondetails"
android:textSize="18sp" />
<Button
android:id="#+id/screens_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#00000000"
android:text="PREVIEW"
android:textColor="#color/blue" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<ImageButton
android:id="#+id/apextheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#drawable/btn_default_holo_dark"
android:paddingBottom="15dp"
android:src="#drawable/apex_1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/apextheme"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/apex"
android:textSize="24dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/apexdetails"
android:textSize="18sp" />
<Button
android:id="#+id/screens_apex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#00000000"
android:text="PREVIEW"
android:textColor="#color/blue" />
</RelativeLayout>
<!-- Nova -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<ImageButton
android:id="#+id/novatheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#drawable/btn_default_holo_dark"
android:paddingBottom="15dp"
android:src="#drawable/nova_1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/novatheme"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/nova"
android:textSize="24dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/novadetails"
android:textSize="18sp" />
<Button
android:id="#+id/screens_nova"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#00000000"
android:text="PREVIEW"
android:textColor="#color/blue" />
</RelativeLayout>
<!-- Adw -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="15dp"
android:paddingTop="15dp" >
<ImageButton
android:id="#+id/adwextheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#drawable/btn_default_holo_dark"
android:paddingBottom="15dp"
android:src="#drawable/adwex_1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/adwextheme"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/adwex"
android:textSize="24dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/adwexdetails"
android:textSize="18sp" />
<Button
android:id="#+id/screens_adwex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#00000000"
android:text="PREVIEW"
android:textColor="#color/blue" />
</RelativeLayout>
<!-- go -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="15dp"
android:paddingTop="15dp" >
<ImageButton
android:id="#+id/gotheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#drawable/btn_default_holo_dark"
android:paddingBottom="15dp"
android:src="#drawable/go_1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/gotheme"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/go"
android:textSize="24dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:fontFamily="sans-serif-light"
android:text="#string/godetails"
android:textSize="18sp" />
<Button
android:id="#+id/screens_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:alpha=".6"
android:background="#00000000"
android:text="PREVIEW"
android:textColor="#color/blue" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
EDIT: Here is the FragmentAdapter.java:
package com.arandompackage.jive;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.viewpagerindicator.IconPagerAdapter;
public class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter{
protected static final String[] CONTENT = new String[] {
"This", "Is", "A", "Test"
};
private int mCount = 4;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getIconResId(int index) {
// TODO Auto-generated method stub
return 0;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment1();
switch(position){
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
}
return fragment;
}
#Override
public int getCount() {
return mCount;
}
#Override
public CharSequence getPageTitle(int position){
String title = "";
switch(position){
case 0:
title = "LAUNCHERS";
break;
case 1:
title = "EXTRAS";
break;
case 2:
title = "CONTACT";
break;
case 3:
title = "HELP";
break;
}
return title;
}
public void setCount(int count){
if (count > 0 && count < 10){
mCount = count;
notifyDataSetChanged();
}
}
}
And the TestFragment:
package com.arandompackage.jive;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TestFragment extends Fragment{
private static final String KEY_CONTENT = "TestFragment:Content";
public static TestFragment newInstance(String content){
TestFragment fragment = new TestFragment();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 20; i++){
builder.append(content).append(" ");
}
builder.deleteCharAt(builder.length() -1);
fragment.mContent = builder.toString();
return fragment;
}
private String mContent = "???";
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)){
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText(mContent);
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(text);
return layout;
}
public void onSavedInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
Here's the twist, the theme still gets applied to Apex Launcher....Any help is much appreciated in advance!
I can't remember my exact answer but this could well be to do with exporting of libraries. Go to project, properties, build path, order and export. The correct order is src, gen, android x.x.x, android private libraries, private libraries. Play around with checking/unchecking them.
I will update the answer when I get home
Edit:
My other SO answer can be found here:
Unfortunately Google Maps API Demos has stopped - Google Maps Android API v2