简体   繁体   中英

Android App crashed and showing a white blank Screen

Note: Sorry if my problem may be really easy to fix, I'm a total beginner in this, I literally started learning this just 2 days ago.

So anyway, I'm making this basic calculator app for androids. I built the .apk and sent it to my phone but it just shows a blank white screen and then it crashes. Here's the xml code and the java code:

     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/numbs" >

<TextView
    android:id="@+id/title"
    android:text="Calculator"
    android:textColor="#E11608"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textSize="35sp"
    android:layout_gravity="top|center"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"

    android:background="#000000"

    />

<EditText
    android:id="@+id/value1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:inputType="number"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:ems="10"
    android:layout_gravity="center"
    android:layout_marginBottom="80dp"
    android:hint="Type in your next value"

    />

<EditText
    android:id="@+id/value2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:inputType="number"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:ems="10"
    android:layout_gravity="center"
    android:layout_marginBottom="148dp"
    android:hint="Type in your first value"
/>

<Button
    android:id="@+id/add"
    android:text="+"
    android:layout_gravity="center|left"
    android:onClick="onClick"
    />
<Button
    android:id="@+id/minus"
    android:text="-"
    android:layout_gravity="center"
    android:layout_marginRight="40dp"
    android:onClick="onClick"
/>
<Button
    android:id="@+id/divide"
    android:text="÷"
    android:layout_gravity="center"
    android:layout_marginLeft="40dp"
    android:onClick="onClick"
/>
<Button
    android:id="@+id/multiply"
    android:text="×"
    android:layout_gravity="center|right"
    android:onClick="onClick"
/>


<TextView
    android:id="@+id/answer"
    android:text="Your answer should display here"
    android:textColor="#E11608"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textSize="10sp"
    android:layout_gravity="bottom|center"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="40dp"
    android:background="#FFFFFF"

/>

</FrameLayout>

Java code:

      package com.Drift.app;

    import android.app.*;
    import android.os.*;
    import java.io.*;
    import android.widget.*;
    import android.view.View.*;
    import android.view.*;

    public class MainActivity extends Activity 
    {

int val1;
int val2;

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

    work();

    }




public void work() 
{
    //create parameter for add button
    Button add = (Button) findViewById(R.id.add);
    Button multiply = (Button) findViewById(R.id.multiply);
    Button minus = (Button) findViewById(R.id.minus);
    Button divide = (Button) findViewById(R.id.divide);

    TextView title = (TextView) findViewById(R.id. title);

    final TextView answer = (TextView) findViewById(R.id. answer);
    final EditText value1 = (EditText) findViewById(R.id. value1);
    final EditText value2 = (EditText) findViewById(R.id. value2);

    val1 = Integer.parseInt(value1.getText().toString());
    final Integer val1 = new Integer(value1.getText().toString());

    val2 = Integer.parseInt(value2.getText().toString());
    final Integer val2 = new Integer(value2.getText().toString());

    add.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            answer.setText(String.valueOf(val1 + val2));
        }
    });
    multiply.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                answer.setText(String.valueOf(val1 * val2));
            }
        });

    minus.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                answer.setText(String.valueOf(val1 - val2));
            }
        });

    divide.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                answer.setText(String.valueOf(val1 / val2));
            }
        });

}


    }

Sorry for not commenting much. Can someone please tell me what I did wrong or maybe propose a better code. Ps sorry again if nearly everything here is wrong but like I said, I'm a total beginner. Thanks.

Okay, thanks for the answers this is is what I ended up with

      package com.Drift.app;

    import android.app.*;
    import android.os.*;
    import java.io.*;
    import android.widget.*;
    import android.view.View.*;
    import android.view.*;

    public class MainActivity extends Activity 
   {

int val1;
int val2;

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

    work();

    }




public void work() 
{
    //create parameter for add button
    Button add = (Button) findViewById(R.id.add);
    Button multiply = (Button) findViewById(R.id.multiply);
    Button minus = (Button) findViewById(R.id.minus);
    Button divide = (Button) findViewById(R.id.divide);

    TextView title = (TextView) findViewById(R.id. title);

    final TextView answer = (TextView) findViewById(R.id. answer);
    final EditText value1 = (EditText) findViewById(R.id. value1);
    final EditText value2 = (EditText) findViewById(R.id. value2);


    if(value1.getText()!=null)
        {val1 = Integer.parseInt(value1.getText().toString());}


    if(value2.getText()!=null)
        {val2 = Integer.parseInt(value2.getText().toString());}



    add.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            answer.setText(String.valueOf(val1 + val2));
        }
    });
    multiply.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {


                answer.setText(String.valueOf(val1 * val2));
            }
        });

    minus.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                answer.setText(String.valueOf(val1 - val2));
            }
        });

    divide.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)

            {

                answer.setText(String.valueOf(val1 / val2));
            }
        });


}


    }

But it's still giving me a blank white page, I'm using gradle by the way


Here's the Android manifest file

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Drift.app" >

<application
    android:allowBackup="true"
    android:icon="@drawable/calc"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Try to check EditText value before use it :

if(value1.getText().toString().trim().length()>0){
    val1 = Integer.parseInt(value1.getText().toString());
 }
 if(value2.getText().toString().trim().length()>0){
    val2 = Integer.parseInt(value2.getText().toString());
 }

It is a probably a problem with a parsing. You are getting value from empty editText. You should not do that in on create method. You should check if value2 and value1 are null before using it.

if(value2.getText()!=null){val2 = Integer.parseInt(value2.getText().toString());}

here is the full working code. i have executed this and its working fine.

ur activity

public class MainAvtivity extends Activity {

    int val1;
    int val2;

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

        work();

    }

    public void work() {

        // create parameter for add button
        Button add = (Button) findViewById(R.id.add);
        Button multiply = (Button) findViewById(R.id.multiply);
        Button minus = (Button) findViewById(R.id.minus);
        Button divide = (Button) findViewById(R.id.divide);

        TextView title = (TextView) findViewById(R.id.title);

        final TextView answer = (TextView) findViewById(R.id.answer);
        final EditText value1 = (EditText) findViewById(R.id.value1);
        final EditText value2 = (EditText) findViewById(R.id.value2);



        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!"".equals(value1.getText().toString())) {
                    val1 = Integer.parseInt(value1.getText().toString());
                }

                if (!"".equals(value2.getText().toString())) {
                    val2 = Integer.parseInt(value2.getText().toString());
                }
                answer.setText(String.valueOf(val1 + val2));
            }
        });
        multiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!"".equals(value1.getText().toString())) {
                    val1 = Integer.parseInt(value1.getText().toString());
                }

                if (!"".equals(value2.getText().toString())) {
                    val2 = Integer.parseInt(value2.getText().toString());
                }
                answer.setText(String.valueOf(val1 * val2));
            }
        });

        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!"".equals(value1.getText().toString())) {
                    val1 = Integer.parseInt(value1.getText().toString());
                }

                if (!"".equals(value2.getText().toString())) {
                    val2 = Integer.parseInt(value2.getText().toString());
                }
                answer.setText(String.valueOf(val1 - val2));
            }
        });

        divide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)

            {
                if (!"".equals(value1.getText().toString())) {
                    val1 = Integer.parseInt(value1.getText().toString());
                }

                if (!"".equals(value2.getText().toString())) {
                    val2 = Integer.parseInt(value2.getText().toString());
                }
                answer.setText(String.valueOf(val1 / val2));
            }
        });

    }

}

and u have not specified height and weight in xml. try this xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/numbs"
    android:gravity="center" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="top|center"
        android:background="#000000"
        android:text="Calculator"
        android:textColor="#E11608"
        android:textSize="35sp" />

    <EditText
        android:id="@+id/value1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:layout_marginBottom="80dp"
        android:background="#FFFFFF"
        android:ems="10"
        android:hint="Type in your next value"
        android:inputType="number" />

    <EditText
        android:id="@+id/value2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:layout_marginBottom="148dp"
        android:background="#FFFFFF"
        android:ems="10"
        android:hint="Type in your first value"
        android:inputType="number" />

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:onClick="onClick"
        android:text="+" />

    <Button
        android:id="@+id/minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginRight="40dp"
        android:onClick="onClick"
        android:text="-" />

    <Button
        android:id="@+id/divide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="40dp"
        android:onClick="onClick"
        android:text="÷" />

    <Button
        android:id="@+id/multiply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|right"
        android:onClick="onClick"
        android:text="×" />

    <TextView
        android:id="@+id/answer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom|center"
        android:layout_marginBottom="40dp"
        android:background="#FFFFFF"
        android:text="Your answer should display here"
        android:textColor="#E11608"
        android:textSize="10sp" />

</FrameLayout>
 java.lang.RuntimeException: Binary XML file line #51: You must supply a layout_width attribute. 

All views must have android:layout_width and android:layout_height defined. At least your Button s don't have either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM