Ronen Rachmani
Ronen Rachmani

Reputation: 31

second activity crashes only on the phone not on the emulator

The first screen upload right and works fine (Toast and all other setting and parameters) until I try to save the result and open the new screen by the same button. Pushing the button crashes the app in my phone without opening the second screen. However in the emulator all work fine.

this is the Main activity:

public class MainActivity extends AppCompatActivity {

    LineChart lineChart1;
    RadioGroup rg1,rg2;
    Button tipBtn;
    Button checkAndCont;
    int [] mSlop ={-1,3};
    int [] b= {-3,3};
    boolean userAnswer1 = false;
    boolean userAnswer2 = false;
    String quesType = "Slop Recognition";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lineChart1 = (LineChart)findViewById(R.id.lineChart1);
        rg1 = (RadioGroup)findViewById(R.id.rg1);
        rg2 = (RadioGroup)findViewById(R.id.rg2);


        tipBtn = (Button)findViewById(R.id.tipBtb);
        tipBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"גרף במערכת צירים נקרא משמאל לימין",Toast.LENGTH_SHORT ).show();
            }
        });

        checkAndCont = (Button)findViewById(R.id.checkAndCont);




        ArrayList<String> xAxes = new ArrayList<>();
        ArrayList<Entry> yAxesLinear1 = new ArrayList<>();
        ArrayList<Entry> yAxesLinear2 = new ArrayList<>();

        int numberDataPoint = 21;
        for (int i=0 ; i<numberDataPoint;i++ ){
            int x=i-10;
            float funLinear1= mSlop[0]*x+b[0];
            float funLinear2= mSlop[1]*x+b[1];
            yAxesLinear1.add(new Entry(x,funLinear1));
            yAxesLinear2.add(new Entry(x,funLinear2));
            //xAxes.add(i,String.valueOf(x));
        }
        //String [] xaxes = new String[xAxes.size()];
        //for (int i=0; i<xAxes.size(); i++){
        //    xaxes[i]=xAxes.get(i);
        //}

        ArrayList<ILineDataSet> lineDataSet= new ArrayList<>();

        LineDataSet lineDataSet1 = new LineDataSet(yAxesLinear1,"פונקציה קווית 1");
        lineDataSet1.setDrawCircles(false);
        lineDataSet1.setColor(Color.CYAN);
        lineDataSet1.setDrawValues(false);
        lineDataSet1.setLineWidth(3);


        LineDataSet lineDataSet2 = new LineDataSet(yAxesLinear2,"פונקציה קווית 2");
        lineDataSet2.setDrawCircles(false);
        lineDataSet2.setColor(Color.GREEN);
        lineDataSet2.setDrawValues(false);
        lineDataSet2.setLineWidth(3);

        lineDataSet.add(lineDataSet1);
        lineDataSet.add(lineDataSet2);

        lineChart1.setData(new LineData(lineDataSet));

        lineChart1.setDrawBorders(true);
        lineChart1.setBorderColor(0xffff00ff);
        lineChart1.setBorderWidth(2);
        lineChart1.setScaleEnabled(true);
        //lineChart1.setDrawGridBackground(true);
        //lineChart1.setGridBackgroundColor(0xffff00ff);
        //lineChart1.setBackgroundColor(0x00000000);

        XAxis xAxis = lineChart1.getXAxis();
        //xAxis.setEnabled(true);
        xAxis.setDrawAxisLine(true);
        xAxis.setAxisLineColor(Color.WHITE);
        lineChart1.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lineChart1.getAxisRight().setEnabled(false);
        lineChart1.getXAxis().setAxisMaximum(10);
        lineChart1.getXAxis().setAxisMinimum(-10);
        lineChart1.getXAxis().setGridColor(0xffff00ff);
        //lineChart1.getXAxis().setDrawAxisLine(true);
        //lineChart1.getXAxis().setAxisLineColor(Color.WHITE); // The buttom limit line of the chart
        //lineChart1.setVisibleXRangeMaximum(50);
        lineChart1.getXAxis().setTextColor(Color.WHITE);
        lineChart1.getXAxis().setTextSize(15);
        LimitLine ll = new LimitLine(0);
        ll.setLineColor(Color.WHITE);
        ll.setLineWidth(1);
        xAxis.addLimitLine(ll);


        YAxis yAxis = lineChart1.getAxisLeft();
        yAxis.setDrawZeroLine(true);
        yAxis.setZeroLineColor(Color.WHITE);// no grid lines
        yAxis.setZeroLineWidth(1); //Sets the line-width of the zero line.
        yAxis.setAxisMinimum(-10f); // start at zero
        yAxis.setAxisMaximum(10f); // the axis maximum is 100
        yAxis.setGridColor(0xffff00ff);
        yAxis.setTextColor(Color.WHITE);
        yAxis.setTextSize(15);

        Legend legend = lineChart1.getLegend();
        legend.setEnabled(true);
        legend.setTextColor(Color.WHITE);
        legend.setTextSize(18);
        legend.setFormSize(13);
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);

        rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton rb1 = (RadioButton) radioGroup.findViewById(i);
                //if(R.id.rg1==i ? userAnswer1==true : userAnswer1==false);
                if (null != rb1 && i > -1) {

                    // checkedId is the RadioButton selected
                    switch (i) {
                        case R.id.rb1:
                            userAnswer1=false;
                            break;

                        case R.id.rb2:
                            userAnswer1=true;
                            break;

                    }
                }
                Toast.makeText(MainActivity.this,Boolean.toString(userAnswer1),Toast.LENGTH_SHORT ).show();
            }
        });

        rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton rb2 = (RadioButton) radioGroup.findViewById(i);
                //if(R.id.rg1==i ? userAnswer1==true : userAnswer1==false);
                if (null != rb2 && i > -1) {

                    // checkedId is the RadioButton selected
                    switch (i) {
                        case R.id.rb3:
                            userAnswer2=false;
                            break;

                        case R.id.rb4:
                            userAnswer2=true;
                            break;

                    }
                }
                Toast.makeText(MainActivity.this,Boolean.toString(userAnswer2),Toast.LENGTH_SHORT ).show();
            }
        });

    }


    public void result(View view) {
        if (isTrue(mSlop[0])==userAnswer1 && isTrue(mSlop[1])==userAnswer2){
            Toast.makeText(MainActivity.this,"תשובה נכונה",Toast.LENGTH_SHORT ).show();
        }else {
            Toast.makeText(MainActivity.this,"לא נכון",Toast.LENGTH_SHORT ).show();
        }
        startActivity(new Intent(MainActivity.this, Main2Activity.class));
    }

    boolean mType;
    public boolean isTrue (int m){
        if (m >0){
        mType=true;
         }else{
            mType=false;
        }
        return mType;
    }
}

this is the related XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:background="@drawable/background_color">

    <com.github.mikephil.charting.charts.LineChart
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:id="@+id/lineChart1">
    </com.github.mikephil.charting.charts.LineChart>


    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/lineChart1"
        android:layout_marginStart="187dp"
        android:paddingRight="15dp"
        android:text="פונקציה 1 עולה"
        android:textColor="#00FFFF"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/tv1"
        android:layout_marginStart="187dp"
        android:paddingRight="15dp"
        android:text="פונקציה 2 עולה"
        android:textColor="#ff00ff00"
        android:textSize="25sp" />

    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/lineChart1"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="#00FFFF"
            android:text="לא נכון"
            android:textColor="#00FFFF"
            android:paddingLeft="2dp"
            android:textSize="25sp"
            />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="#00FFFF"
            android:text="נכון"
            android:textColor="#00FFFF"
            android:paddingLeft="2dp"
            android:textSize="25sp"
            android:layout_marginLeft="5sp"/>
    </RadioGroup>

    <RadioGroup
        android:id="@+id/rg2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/rg1"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="#ff00ff00"
            android:text="לא נכון"
            android:textColor="#ff00ff00"
            android:paddingLeft="2dp"
            android:textSize="25sp"
            />

        <RadioButton
            android:id="@+id/rb4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="#ff00ff00"
            android:text="נכון"
            android:textColor="#ff00ff00"
            android:paddingLeft="2dp"
            android:textSize="25sp"
            android:layout_marginLeft="5dp"/>
    </RadioGroup>

    <Button
        android:id="@+id/tipBtb"
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/rg2"
        android:layout_marginStart="66dp"
        android:layout_marginTop="11dp"
        android:scaleType="fitCenter"
        android:text="TIP" />

    <Button
        android:id="@+id/checkAndCont"
        android:text="שלח"
        android:layout_width="80sp"
        android:layout_height="50sp"
        android:layout_marginTop="11dp"
        android:layout_below="@+id/tv2"
        android:layout_centerHorizontal="true"
        android:onClick="result"/>

</RelativeLayout>

this is the second activity:

public class Main2Activity extends AppCompatActivity {

    LineChart lineChart2;
    Button quickTipBtn,sendAndNext;
    TextView question1,question2;
    EditText answer1,answer2;
    final int [] mSlop ={1,3};
    final int [] b= {1,3};
    private boolean userAnswer1 = false;
    private boolean userAnswer2 = false;
    final String quesType = "y-intercept Recognition";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        question1 = (TextView)findViewById(R.id.question1);
        question2 = (TextView)findViewById(R.id.question2);
        answer1 = (EditText)findViewById(R.id.answer1);
        answer2 = (EditText)findViewById(R.id.answer2);
        quickTipBtn =(Button)findViewById(R.id.quickTipBtn);
        quickTipBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(Main2Activity.this,"קואורדינטת החיתוך עם ציר Y ו b - שווים",Toast.LENGTH_SHORT ).show();
            }
        });
        sendAndNext = (Button)findViewById(R.id.sendAndNext);


        lineChart2 = (LineChart)findViewById(R.id.lineChart2);
        ArrayList<String> xAxes = new ArrayList<>();
        ArrayList<Entry> yAxesLinear1 = new ArrayList<>();
        ArrayList<Entry> yAxesLinear2 = new ArrayList<>();

        int numberDataPoint = 21;
        for (int i=0 ; i<numberDataPoint;i++ ){
            int x=i-10;
            float funLinear1= mSlop[0]*x+b[0];
            float funLinear2= mSlop[1]*x+b[1];
            yAxesLinear1.add(new Entry(x,funLinear1));
            yAxesLinear2.add(new Entry(x,funLinear2));
            //xAxes.add(i,String.valueOf(x));
        }
        //String [] xaxes = new String[xAxes.size()];
        //for (int i=0; i<xAxes.size(); i++){
        //    xaxes[i]=xAxes.get(i);
        //}

        ArrayList<ILineDataSet> lineDataSet= new ArrayList<>();

        LineDataSet lineDataSet1 = new LineDataSet(yAxesLinear1,"פונקציה קווית 1");
        lineDataSet1.setDrawCircles(false);
        lineDataSet1.setColor(Color.CYAN);
        lineDataSet1.setDrawValues(false);
        lineDataSet1.setLineWidth(3);


        LineDataSet lineDataSet2 = new LineDataSet(yAxesLinear2,"פונקציה קווית 2");
        lineDataSet2.setDrawCircles(false);
        lineDataSet2.setColor(Color.GREEN);
        lineDataSet2.setDrawValues(false);
        lineDataSet2.setLineWidth(3);

        lineDataSet.add(lineDataSet1);
        lineDataSet.add(lineDataSet2);

        lineChart2.setData(new LineData(lineDataSet));

        lineChart2.setDrawBorders(true);
        lineChart2.setBorderColor(0xffff00ff);
        lineChart2.setBorderWidth(2);
        lineChart2.setScaleEnabled(true);
        //lineChart1.setDrawGridBackground(true);
        //lineChart1.setGridBackgroundColor(0xffff00ff);
        //lineChart1.setBackgroundColor(0x00000000);

        XAxis xAxis = lineChart2.getXAxis();
        //xAxis.setEnabled(true);
        xAxis.setDrawAxisLine(true);
        xAxis.setAxisLineColor(Color.WHITE);
        lineChart2.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lineChart2.getAxisRight().setEnabled(false);
        lineChart2.getXAxis().setAxisMaximum(10);
        lineChart2.getXAxis().setAxisMinimum(-10);
        lineChart2.getXAxis().setGridColor(0xffff00ff);
        //lineChart1.getXAxis().setDrawAxisLine(true);
        //lineChart1.getXAxis().setAxisLineColor(Color.WHITE); // The buttom limit line of the chart
        //lineChart1.setVisibleXRangeMaximum(50);
        lineChart2.getXAxis().setTextColor(Color.WHITE);
        lineChart2.getXAxis().setTextSize(15);
        LimitLine ll = new LimitLine(0);
        ll.setLineColor(Color.WHITE);
        ll.setLineWidth(1);
        xAxis.addLimitLine(ll);


        YAxis yAxis = lineChart2.getAxisLeft();
        yAxis.setDrawZeroLine(true);
        yAxis.setZeroLineColor(Color.WHITE);// no grid lines
        yAxis.setZeroLineWidth(1); //Sets the line-width of the zero line.
        yAxis.setAxisMinimum(-10f); // start at zero
        yAxis.setAxisMaximum(10f); // the axis maximum is 100
        yAxis.setGridColor(0xffff00ff);
        yAxis.setTextColor(Color.WHITE);
        yAxis.setTextSize(15);

        Legend legend = lineChart2.getLegend();
        legend.setEnabled(true);
        legend.setTextColor(Color.WHITE);
        legend.setTextSize(18);
        legend.setFormSize(13);
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);


    }

    public void sendAndNext(View view) {
        String temValue1= answer1.getText().toString();
        //Toast.makeText(secondScreen.this, answer1.getText(),Toast.LENGTH_LONG).show();
        String temValue2= answer2.getText().toString();
        if (Integer.parseInt(temValue1)==mSlop[0] && Integer.parseInt(temValue2)==mSlop[1]){
            Toast.makeText(Main2Activity.this, "תשובה נכונה",Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(Main2Activity.this, "תשובה לא נכונה או שלא הוזנו נתונים",Toast.LENGTH_LONG).show();
        }
    }
}

this is the second XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5"
    android:gravity="top"
    android:paddingTop="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:background="@drawable/background_color2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:paddingTop="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp">
        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/lineChart2"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            >
        </com.github.mikephil.charting.charts.LineChart>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:paddingTop="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:orientation="horizontal"
        android:weightSum="2"
        android:gravity="right">

        <EditText
            android:id="@+id/answer1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:digits="0123456789."
            android:inputType="numberSigned"
            android:maxLength="2"
            android:hint="הכנס מספר"
            android:textColorHint="#78ffd6"
            android:textSize="18dp"
            android:textColor="#00FFFF"
            />
        <TextView
            android:id="@+id/question1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:paddingRight="5dp"
            android:paddingLeft="5dp"
            android:text="למה שווה הפרמטר b ?"
            android:textColor="#00FFFF"
            android:textSize="25sp"
            >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:paddingTop="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:orientation="horizontal"
        android:weightSum="2"
        android:gravity="right"
        >
        <EditText
            android:id="@+id/answer2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:digits="0123456789."
            android:maxLength="2"
            android:hint="הכנס מספר"
            android:textColorHint="#64f38c"
            android:textSize="18dp"
            android:inputType="numberSigned"
            android:textColor="#ff00ff00"
            />
        <TextView
            android:id="@+id/question2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:text="מהי נק' החיתוך עם ציר Y?"
            android:textColor="#ff00ff00"
            android:textSize="25sp"
            >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:paddingTop="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:orientation="horizontal"
        android:weightSum="2"
        android:gravity="center">
        <Button
            android:layout_marginRight="40dp"
            android:id="@+id/quickTipBtn"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Quick Tip"
            android:background="@android:color/transparent"
            android:textColor="#f05053"

            />
        <Button
            android:id="@+id/sendAndNext"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Next One"
            android:background="@android:color/transparent"
            android:textColor="#ffafbd"
            android:onClick="sendAndNext"/>
    </LinearLayout>
</LinearLayout>

this is the Logcat error:

FATAL EXCEPTION: main
Process: com.example.rachmani.mythematix_linears, PID: 18087 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rachmani.mythematix_linears/com.example.rachmani.mythematix_linears.Main2Activity}: android.view.InflateException: Binary XML file line #0: Error inflating class

Upvotes: 2

Views: 82

Answers (2)

Ronen Rachmani
Ronen Rachmani

Reputation: 31

Thanks for your patience... After a close review of the error log, I've indicated the next error:

com.example.rachmani.mythematix_linears:drawable/background_color2" (7f060055) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060055 a=-1 r=0x7f060055}

The original drawable was:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:startColor="#0f0c29"
                android:endColor="#302b63"
                android:type="linear"
                android:angle="90"/>
        </shape>
    </item>
</selector>

Just changed it to:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:startColor="#141e30"
                android:endColor="#243b55"
                android:type="linear"
                android:angle="90"/>
        </shape>
    </item>
</selector>

It's seems that the gradient background I tried to use make the problem... Thanks all for your support.

Upvotes: 1

Sandecoder
Sandecoder

Reputation: 154

Please check your second XML file for a closing </LinearLayout> tag in the end.

From the code snippet you have posted,it is evident that you haven't closed the first LinearLayout tag. Hope it helps!

Upvotes: 0

Related Questions