My program crashes when the button is clicked.. why? - java

LOGCAT
04-03 20:59:46.189: E/AndroidRuntime(362): android.content.res.Resources$NotFoundException: String resource ID #0x0
04-03 20:59:46.189: E/AndroidRuntime(362): at android.content.res.Resources.getText(Resources.java:201)
04-03 20:59:46.189: E/AndroidRuntime(362): at android.widget.TextView.setText(TextView.java:2857)
04-03 20:59:46.189: E/AndroidRuntime(362): at coin.calc.wilson.CoinCalculatorActivity$1.onClick(CoinCalculatorActivity.java:65)
04-03 20:59:46.189: E/AndroidRuntime(362): at android.view.View.performClick(View.java:2485)
04-03 20:59:46.189: E/AndroidRuntime(362): at android.view.View$PerformClick.run(View.java:9080)
04-03 20:59:46.189: E/AndroidRuntime(362): at android.os.Handler.handleCallback(Handler.java:587)
04-03 20:59:46.189: E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 20:59:46.189: E/AndroidRuntime(362): at android.os.Looper.loop(Looper.java:123)
04-03 20:59:46.189: E/AndroidRuntime(362): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 20:59:46.189: E/AndroidRuntime(362): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 20:59:46.189: E/AndroidRuntime(362): at java.lang.reflect.Method.invoke(Method.java:507)
04-03 20:59:46.189: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 20:59:46.189: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 20:59:46.189: E/AndroidRuntime(362): at dalvik.system.NativeStart.main(Native Method)
04-03 20:59:49.720: I/Process(362): Sending signal. PID: 362 SIG: 9
Here is my java code:
package coin.calc.wilson;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class CoinCalculatorActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button solve = (Button)findViewById(R.id.bsolve);
solve.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int td, tc, twenty, ten, five, one, quarter, dime, nickel, penny;
EditText tdet = (EditText)findViewById(R.id.etdollars);
EditText tcet = (EditText)findViewById(R.id.etcents);
try{
td = Integer.parseInt(tdet.getText().toString());
tc = Integer.parseInt(tdet.getText().toString());
}
catch (NumberFormatException e) {
td = tc = 0;
}
if (td < 0 || tc < 0){
/* ERROR */
}
else{
if( td == 0 ){
twenty = ten = five = one = 0;
}
else{
one = td%5;
if(td >= 20){
twenty = (td/20)-((td%20)/20);
}
else{
twenty = 0;
}
int tda20 = td-(20*twenty);
if (tda20 >= 10){
ten = (tda20/10)-((tda20%10)/10);
}
else{
ten = 0;
}
int tda10 = tda20-(10*ten);
if(tda10>=5){
five = (tda10/5)-((tda10%5)/5);
}
else{
five = 0;
}
TextView tv20 = (TextView)findViewById(R.id.tvtwenty);
tv20.setText(twenty);
TextView tv10 = (TextView)findViewById(R.id.tvten);
tv10.setText(ten);
TextView tv5 = (TextView)findViewById(R.id.tvfive);
tv5.setText(five);
TextView tv1 = (TextView)findViewById(R.id.tvone);
tv1.setText(one);
}
if( tc == 0 ){
quarter = dime = nickel = penny = 0;
}
else{
penny = tc%5;
if(tc >= 25){
quarter = (tc/25)-((td%25)/25);
}
else{
quarter = 0;
}
int tcaq = tc-(25*quarter);
if (tcaq >= 10){
dime = (tcaq/10)-((tcaq%10)/10);
}
else{
dime = 0;
}
int tcad = tcaq-(10*ten);
if(tcad>=5){
nickel = (tcad/5)-((tcad%5)/5);
}
else{
nickel = 0;
}
TextView tvq = (TextView)findViewById(R.id.tvquarter);
tvq.setText(quarter);
TextView tvd = (TextView)findViewById(R.id.tvdime);
tvd.setText(dime);
TextView tvn = (TextView)findViewById(R.id.tvnickel);
tvn.setText(nickel);
TextView tvp = (TextView)findViewById(R.id.tvpenny);
tvp.setText(penny);
}
}
}});}
}
and here is the xml:
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:gravity="center" >
<EditText
android:id="#+id/etdollars"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dollars"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<EditText
android:id="#+id/etcents"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cents"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center" >
<Button
android:id="#+id/bsolve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Solve!" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="80dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_weight="0.00"
android:layout_marginLeft="8.75dp"
android:layout_marginRight="8.75dp"
android:src="#drawable/twenty" />
<TextView
android:id="#+id/tvtwenty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:layout_gravity="center_vertical"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="61dp"
android:layout_height="80dp"
android:layout_marginLeft="43dp"
android:layout_marginRight="13.5dp"
android:src="#drawable/cquarter" />
<TextView
android:id="#+id/tvquarter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:layout_gravity="center_vertical"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="80dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_weight="0.00"
android:layout_marginLeft="8.75dp"
android:layout_marginRight="8.75dp"
android:src="#drawable/ten" />
<TextView
android:id="#+id/tvten"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:layout_gravity="center_vertical"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="61dp"
android:layout_height="80dp"
android:layout_marginLeft="43dp"
android:layout_marginRight="13.5dp"
android:src="#drawable/cdime" />
<TextView
android:id="#+id/tvdime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:layout_gravity="center_vertical"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="80dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_weight="0.00"
android:layout_marginLeft="8.75dp"
android:layout_marginRight="8.75dp"
android:src="#drawable/five" />
<TextView
android:id="#+id/tvfive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:layout_gravity="center_vertical"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="61dp"
android:layout_height="80dp"
android:layout_marginLeft="43dp"
android:layout_marginRight="13.5dp"
android:src="#drawable/cnickel" />
<TextView
android:id="#+id/tvnickel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:layout_gravity="center_vertical"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="80dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_weight="0.00"
android:layout_marginLeft="8.75dp"
android:layout_marginRight="8.75dp"
android:src="#drawable/one" />
<TextView
android:id="#+id/tvone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:layout_gravity="center_vertical"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="61dp"
android:layout_height="80dp"
android:layout_marginLeft="43dp"
android:layout_marginRight="13.5dp"
android:src="#drawable/cpenny" />
<TextView
android:id="#+id/tvpenny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:layout_gravity="center_vertical"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</TableLayout>
If it helps, i think the error MAY have something to do with resources i am using (eight jpgs), but i'm not sure. The program runs cleanly up until the OnClickListener is activated. Thanks for any and all help!

Is here (and in another lines like that):
tv20.setText(twenty);
twenty is int and should be String (I suppose that you want to show the number in the textview):
tv20.setText(String.valueOf(twenty));
EDIT:
All this variables have the same error
int td, tc, twenty, ten, five, one, quarter, dime, nickel, penny;

You have a small but heavy mistake:
If you try to put a number/integer into a TextView, you need to cast it to a String first. If you don't do that, Android things you reference to an internal id like R.string.mynumber and that is not what you want.

try moving the two following lines right underneath Button solve = (Button)findViewById(R.id.bsolve); and before solve.setOnClickListener(new OnClickListener()
EditText tdet = (EditText)findViewById(R.id.etdollars);
EditText tcet = (EditText)findViewById(R.id.etcents);

Related

How to fix ViewPager to prevent app to getting error and crash?

i'm working on login page, based on Androidhive tutorial and problem is Viewpager work fine with 2 layout but for 3 layouts app gonna crash.
In androidhive tutorial used 2 layout but i want to use 3 because i'm getting sms then otp then register, so problem is with 3rd layout which it won't appear and crash.
Androidhive Tutorial Link:
Part 1 Part 2
Here are the codes :
XML
<LinearLayout
android:id="#+id/layout_phone_registration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="25dp"
android:layout_marginTop="100dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:gravity="center_horizontal"
android:inputType="textCapWords"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="#string/msg_enter_mobile"
android:textColor="#android:color/white"
android:textSize="14dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/inputMobile"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:fontFamily="sans-serif-light"
android:hint="#string/lbl_mobile"
android:inputType="phone"
android:maxLength="12"
android:padding="5dp"
android:textColor="#color/colorPrimary"
android:textCursorDrawable="#null"
android:textSize="18dp" />
<Button
android:id="#+id/btn_request_sms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:text="#string/lbl_next"
android:textColor="#android:color/white"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_otp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="25dp"
android:layout_marginTop="100dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center_horizontal"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="#string/msg_sit_back"
android:textColor="#android:color/white"
android:textSize="16dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:gravity="center_horizontal"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="#string/msg_manual_otp"
android:textColor="#android:color/white"
android:textSize="12dp" />
<EditText
android:id="#+id/inputOtp"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:hint="#string/lbl_enter_otp"
android:inputType="number"
android:maxLength="6"
android:padding="10dp"
android:textCursorDrawable="#null"
android:textSize="18dp" />
<Button
android:id="#+id/btn_verify_otp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="#string/lbl_submit"
android:textColor="#android:color/white"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_complete_registration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical">
<EditText
android:id="#+id/inputName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="#android:color/white"
android:fontFamily="sans-serif-light"
android:hint="#string/lbl_name"
android:padding="5dp"
android:singleLine="true"
android:textColor="#color/colorPrimary"
android:textSize="18dp" />
<EditText
android:id="#+id/inputAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="#android:color/white"
android:fontFamily="sans-serif-light"
android:hint="#string/lbl_email"
android:padding="5dp"
android:textColor="#color/colorPrimary"
android:textSize="18dp" />
<Button
android:id="#+id/btn_register_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:text="#string/lbl_next"
android:textColor="#android:color/white"
android:textSize="14dp" />
</LinearLayout>
</ir.atlaspio.atlasdrinkingservice.AdvancedUI.MyViewPager>>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginBottom="60dp"
android:indeterminateTint="#color/colorAccent"
android:indeterminateTintMode="src_atop"
android:visibility="gone" />
<LinearLayout
android:id="#+id/layout_edit_mobile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_edit_mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="16dp" />
<ImageButton
android:id="#+id/btn_edit_mobile"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="#null"
android:src="#drawable/ic_exit_to_app_black_24dp" />
</LinearLayout>
Java
private ViewPager viewPager;
private ViewPagerAdapter adapter;
OnCreate =>
viewPager.setCurrentItem(0);
adapter = new ViewPagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Public Class Activity =>
class ViewPagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return 3;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
public Object instantiateItem(View collection, int position) {
int resId = 0;
switch (position) {
case 0:
resId = R.id.layout_phone_registration;
break;
case 1:
resId = R.id.layout_otp;
break;
case 2:
resId = R.id.layout_complete_registration;
break;
}
return findViewById(resId);
}
}
Logcat
08-29 10:48:12.426 4499-4499/ir.atlaspio.atlasdrinkingservice
E/InputEventSender: Exception dispatching finished signal.
08-29 10:48:12.426 4499-4499/ir.atlaspio.atlasdrinkingservice E/MessageQueue-JNI: Exception in MessageQueue callback:
handleReceiveCallback
08-29 10:48:12.436 4499-4499/ir.atlaspio.atlasdrinkingservice E/MessageQueue-JNI: java.lang.UnsupportedOperationException: Required
method destroyItem was not overridden
at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:201)
at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:128)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1172)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:617)
at android.support.v4.view.ViewPager.pageRight(ViewPager.java:2888)
at android.support.v4.view.ViewPager.arrowScroll(ViewPager.java:2844)
at android.support.v4.view.ViewPager.executeKeyEvent(ViewPager.java:2764)
at android.support.v4.view.ViewPager.dispatchKeyEvent(ViewPager.java:2738)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3876)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3850)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3549)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3606)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3582)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3742)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.
As per your error
java.lang.UnsupportedOperationException: Required method destroyItem was not overridden
You need to add this method in your pager adapter
#Override
void destroyItem (ViewGroup container, int position, Object object){
((ViewPager) container).removeView((View) object);
// more code if needed
}
Override destroyItem method in your ViewPagerAdapter class and remove obj as container.removeView(obj as LinearLayout) where LinearLayout is your root layout
#Override
void destroyItem(ViewGroup container, int position, Object obj) {
container.removeView(obj as LinearLayout)
}

Android: 3 activities with the same code but 1 crashes all the time

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!

Could not execute method for android:onClick

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.

FATAL EXCEPTION: main,able to start activity ComponentInfo,java.lang.IndexOutOfBoundsException

I'm a beginner programmer and I've started a web crawler application from a weather site. When I run the application while the first two cities of each Activity are working normally, the others stops the application with the following message: "Current_Weather was interrupted. Please help me if there is a solution to my problem.
Error Message :
06-24 16:03:04.805 18004-18004/com.example.junior_marg.current_weather E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.junior_marg.current_weather, PID: 18004
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.junior_marg.current_weather/com.example.junior_marg.current_weather.Main14Activity}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.junior_marg.current_weather.Main14Activity.onCreate(Main14Activity.java:94)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
Java Code:
package com.example.junior_marg.current_weather;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class Main14Activity extends AppCompatActivity {
TextView mTextView_33;
TextView mTextView_34;
TextView mTextView_35;
TextView mTextView_36;
TextView mTextView_37;
TextView mTextView_38;
TextView mTextView_39;
TextView mTextView_40;
TextView mTextView_41;
TextView mTextView_42;
TextView mTextView_43;
TextView mTextView_44;
TextView mTextView_45;
TextView mTextView_46;
TextView mTextView_47;
TextView mTextView_48;
String therm_3;
String ygrasia_3;
String simdrosou_3;
String anemos_3;
String varom_3;
String simerinosy_3;
String ragd_3;
String trexkak_3;
String miniaiosy_3;
String ethsiosy_3;
String aisthpsix_3;
String yperithriakt_3;
String deiktisdis_3;
String hliakiakt_3;
String anatoli_3;
String disi_3;
String url;
Document doc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main14);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mTextView_33 = (TextView) findViewById(R.id.textview77);
mTextView_34 = (TextView) findViewById(R.id.textview79);
mTextView_35 = (TextView) findViewById(R.id.textview81);
mTextView_36 = (TextView) findViewById(R.id.textview83);
mTextView_37 = (TextView) findViewById(R.id.textview85);
mTextView_38 = (TextView) findViewById(R.id.textview87);
mTextView_39 = (TextView) findViewById(R.id.textview89);
mTextView_40 = (TextView) findViewById(R.id.textview91);
mTextView_41 = (TextView) findViewById(R.id.textview93);
mTextView_42 = (TextView) findViewById(R.id.textview95);
mTextView_43 = (TextView) findViewById(R.id.textview97);
mTextView_44 = (TextView) findViewById(R.id.textview99);
mTextView_45 = (TextView) findViewById(R.id.textview101);
mTextView_46 = (TextView) findViewById(R.id.textview103);
mTextView_47 = (TextView) findViewById(R.id.textview105);
mTextView_48 = (TextView) findViewById(R.id.textview107);
doc = null;
url = "http://penteli.meteo.gr/stations/larissa/";
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
}
Element table_33 = doc.select("table").get(0);
Elements rows_33 = table_33.select("tr");
Element row_33 = rows_33.get(5);
Elements cols_33 = row_33.select("td");
therm_3 = cols_33.get(1).text() ;
mTextView_33.setText(therm_3);
Element row_34 = rows_33.get(6);
Elements cols_34= row_34.select("td");
ygrasia_3 = cols_34.get(1).text() ;
mTextView_34.setText(ygrasia_3);
Element row_35 = rows_33.get(7);
Elements cols_35 = row_35.select("td");
simdrosou_3 = cols_35.get(1).text() ;
mTextView_35.setText(simdrosou_3);
Element row_36 = rows_33.get(8);
Elements cols_36 = row_36.select("td");
anemos_3 = cols_36.get(1).text() ;
mTextView_36.setText(anemos_3);
Element row_37 = rows_33.get(9);
Elements cols_37 = row_37.select("td");
varom_3 = cols_37.get(1).text() ;
mTextView_37.setText(varom_3);
Element row_38 = rows_33.get(10);
Elements cols_38 = row_38.select("td");
simerinosy_3 = cols_38.get(1).text() ;
mTextView_38.setText(simerinosy_3);
Element row_39 = rows_33.get(11);
Elements cols_39 = row_39.select("td");
ragd_3 = cols_39.get(1).text() ;
mTextView_39.setText(ragd_3);
Element row_40 = rows_33.get(12);
Elements cols_40 = row_40.select("td");
trexkak_3 = cols_40.get(1).text() ;
mTextView_40.setText(trexkak_3);
Element row_41 = rows_33.get(13);
Elements cols_41 = row_41.select("td");
miniaiosy_3 = cols_41.get(1).text() ;
mTextView_41.setText(miniaiosy_3);
Element row_42 = rows_33.get(14);
Elements cols_42 = row_42.select("td");
ethsiosy_3 = cols_42.get(1).text() ;
mTextView_42.setText(ethsiosy_3);
Element row_43 = rows_33.get(15);
Elements cols_43 = row_43.select("td");
aisthpsix_3 = cols_43.get(1).text() ;
mTextView_43.setText(aisthpsix_3);
Element row_44 = rows_33.get(16);
Elements cols_44 = row_44.select("td");
yperithriakt_3 = cols_44.get(1).text() ;
mTextView_44.setText(yperithriakt_3);
Element row_45 = rows_33.get(17);
Elements cols_45 = row_45.select("td");
deiktisdis_3 = cols_45.get(1).text() ;
mTextView_45.setText(deiktisdis_3);
Element row_46 = rows_33.get(18);
Elements cols_46 = row_46.select("td");
hliakiakt_3 = cols_46.get(1).text() ;
mTextView_46.setText(hliakiakt_3);
Element row_47 = rows_33.get(19);
Elements cols_47 = row_47.select("td");
anatoli_3 = cols_47.get(1).text() ;
mTextView_47.setText(anatoli_3);
Element row_48 = rows_33.get(20);
Elements cols_48 = row_48.select("td");
disi_3 = cols_48.get(1).text() ;
mTextView_48.setText(disi_3);
}
}
XML Code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.junior_marg.current_weather.Main14Activity">
<TextView
android:id="#+id/textview76"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/c"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview77"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview76"
android:layout_marginLeft="20dp"
android:layout_marginStart="92dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview78"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/d"
android:layout_below="#+id/textview76"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview79"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview78"
android:layout_below="#+id/textview77"
android:layout_marginLeft="50dp"
android:layout_marginStart="133dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/h2"
android:layout_below="#+id/textview78"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview81"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview80"
android:layout_marginLeft="20dp"
android:layout_marginStart="77dp"
android:layout_marginTop="63dp" />
<TextView
android:id="#+id/textview82"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/i2"
android:layout_below="#+id/textview80"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview83"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview82"
android:layout_marginLeft="50dp"
android:layout_marginStart="130dp"
android:layout_marginTop="90dp" />
<TextView
android:id="#+id/textview84"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/j2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textview82"/>
<TextView
android:id="#+id/textview85"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview84"
android:layout_below="#+id/textview83"
android:layout_marginLeft="10dp"
android:layout_marginStart="104dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview86"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/k2"
android:layout_below="#+id/textview84"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview87"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview86"
android:layout_below="#+id/textview85"
android:layout_marginLeft="50dp"
android:layout_marginStart="63dp"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/textview88"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/l2"
android:layout_below="#+id/textview86"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview89"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview88"
android:layout_below="#+id/textview87"
android:layout_marginLeft="20dp"
android:layout_marginStart="94dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview90"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/m2"
android:layout_below="#+id/textview88"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview91"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview90"
android:layout_below="#+id/textview89"
android:layout_marginLeft="50dp"
android:layout_marginStart="36dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview92"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/n2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textview90"/>
<TextView
android:id="#+id/textview93"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview92"
android:layout_below="#+id/textview91"
android:layout_marginLeft="20dp"
android:layout_marginStart="75dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview94"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/o2"
android:layout_below="#+id/textview92"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview95"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview94"
android:layout_below="#+id/textview93"
android:layout_marginLeft="50dp"
android:layout_marginStart="85dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview96"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/p2"
android:layout_below="#+id/textview94"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview97"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview96"
android:layout_below="#+id/textview95"
android:layout_marginLeft="20dp"
android:layout_marginStart="73dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview98"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/q2"
android:layout_below="#+id/textview96"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview99"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview98"
android:layout_below="#+id/textview97"
android:layout_marginLeft="50dp"
android:layout_marginStart="24dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/r2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textview98"/>
<TextView
android:id="#+id/textview101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview100"
android:layout_below="#+id/textview99"
android:layout_marginLeft="10dp"
android:layout_marginStart="52dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview102"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/s2"
android:layout_below="#+id/textview100"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview103"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview102"
android:layout_below="#+id/textview101"
android:layout_marginLeft="50dp"
android:layout_marginStart="45dp"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/textview104"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/t2"
android:layout_below="#+id/textview102"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview105"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview104"
android:layout_below="#+id/textview103"
android:layout_marginLeft="20dp"
android:layout_marginStart="122dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview106"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/u2"
android:layout_below="#+id/textview104"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview107"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview106"
android:layout_below="#+id/textview105"
android:layout_marginLeft="50dp"
android:layout_marginStart="151dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
I think your problem is in therm_3 = cols_33.get(1).text() ;. it seems that your cols_33 just has one element while you are trying to get it's second element. I hope your problem would be solved if you replace that line with this:
therm_3 = cols_33.get(0).text() ;

java.lang.NullPointerException in my app, creating and inserting a sqlite database, why?

I want to make a Sqlite connection for store data in my app. The problem is that when I execute, the program fail and appear a "java.lang.NullPointerException".
Sorry for not being able to express myself well in English,this is the first time that I post in this page.
LOGCAT PIECE ERROR
04-08 23:15:47.333 7307-7307/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
**java.lang.NullPointerException
at com.example.app.formularioanuncio$1.onClick(formularioanuncio.java:40)**
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17477)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
UPDATED:the first problem(nullpointedexception) has been resolved, Now I have this error:
04-09 00:44:09.845 8746-8746/com.example.app E/SQLiteLog﹕ (1) table anuncios has no column named titulo
04-09 00:44:09.850 8746-8746/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: table anuncios has no column named titulo (code 1): , while compiling: insert into anuncios (titulo,descripcion,unidades,precioini,descuento,preciofi) values (?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:992)
at com.example.app.conexion.insertaranuncio(conexion.java:33)
at com.example.app.formularioanuncio$1.onClick(formularioanuncio.java:60)
FORM CLASS
public class formularioanuncio extends ActionBarActivity {
EditText titu_anun, descrip_anun, unidades_anun, precioini_anun, descuento_anun, preciofi_anun;
Button but_public_anun;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formularioanuncio);
titu_anun = (EditText) findViewById(R.id.etxt_tituloanuncio);
descrip_anun = (EditText) findViewById(R.id.etxt_descrip_anun);
unidades_anun = (EditText) findViewById(R.id.etxt_unidad_anun);
precioini_anun = (EditText) findViewById(R.id.etxt_precioini_anun);
descuento_anun = (EditText) findViewById(R.id.etxt_descuento_anun);
preciofi_anun = (EditText) findViewById(R.id.etxt_preciofi_anun);
but_public_anun = (Button) findViewById(R.id.btn_publi_anun);
but_public_anun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String titulo = titu_anun.getText().toString();
String descripcion = descrip_anun.getText().toString();
String unidades = unidades_anun.getText().toString();
String precio_inicial = precioini_anun.getText().toString();
String descuento = descuento_anun.getText().toString();
String precio_final = preciofi_anun.getText().toString();
conexion cn = new conexion(getApplicationContext(),"DBanuncios.db", null, 1);
SQLiteDatabase db = cn.getWritableDatabase();
cn.insertaranuncio(db,titulo,descripcion,unidades,precio_inicial,
descuento,precio_final);//logcat error suggest here
}
});
}
LAYOUT FORM CLASS (layout.activity_formularioanuncio)
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout"
android:layout_marginBottom="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/tituloanuncio"
android:id="#+id/txttituloanuncio"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/etxt_titu_anun"
android:layout_below="#+id/etxt_tituloanuncio"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/descripcionanuncio"
android:id="#+id/txtdescripanuncio"
android:layout_marginTop="30dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/etxt_descrip_anun" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/unidadesanuncio"
android:id="#+id/txt_unidadesanuncio"
android:layout_marginTop="30dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/etxt_unidad_anun"
android:layout_gravity="center_horizontal"
android:inputType="none" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/precioinianuncio"
android:id="#+id/txtprecioini_anuncio"
android:layout_marginTop="30dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:ems="10"
android:id="#+id/etxt_precioini_anun" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/descuentoanuncio"
android:id="#+id/txtdescuentoanun"
android:layout_marginTop="30dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:ems="10"
android:id="#+id/etxt_descuento_anun" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/preciofinanuncio"
android:id="#+id/txt_preciofinanuncio"
android:layout_marginTop="30dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/etxt_preciofi_anun" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/botonpublicaranuncio"
android:id="#+id/btn_publi_anun"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textSize="28sp"
android:paddingTop="15dp"
android:paddingBottom="15dp" />
</LinearLayout>
</ScrollView>
SQLITE CONNECTION CLASS
public class conexion extends SQLiteOpenHelper {
String sql="create table anuncios (id integer primary key autoincrement, titulo text,descripcion text,unidades text,precioini text,descuento text,preciofi text)";
public conexion(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
}
public void insertaranuncio (SQLiteDatabase db,String titulo,String descripcion,String unidades,String precioini,String descuento,String preciofi){
SQLiteStatement pst=db.compileStatement("insert into anuncios (titulo,descripcion,unidades,precioini,descuento,preciofi)"+
"values (?,?,?,?,?,?)"); ///logcat error suggest here
pst.bindString(1,titulo);
pst.bindString(2,descripcion);
pst.bindString(3,unidades);
pst.bindString(4,precioini);
pst.bindString(5,descuento);
pst.bindString(6,preciofi);
pst.execute();
}
}
Either titu_anun is null or titu_anun.getText() returns null. Use the debugger or some log messages to figure out which one is null.
Edit: See ZouZou's comment on your question for the quick answer.
You have no view with ID R.id.etxt_tituloanuncio so your EditText titu_anun is null and calling getText() on a null object throws the Null Pointer Exception

Categories

Resources