当我尝试在模拟器上运行我的应用程序时,很遗憾地说应用已停止

时间:2015-12-21 17:06:03

标签: java android xml

我是android的新手。我刚刚制作了第一个应用程序。但是当我在Android模拟器上运行我的应用程序时,它不会运行。应用程序编译没有任何错误,但在模拟器上它显示不幸的应用程序停止。

这是java代码:

package com.example.kartikeya;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
double x,y;
static int op=0;
double result;
TextView textView = new TextView(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView();

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this); // calling onClick() method
    Button button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(this);
    //Button button3 = (Button) findViewById(R.id.button3);
   // button3.setOnClickListener(this);


    textView = (TextView) findViewById(R.id.textView);
    textView.setOnClickListener(this);
}

private void setContentView() {
}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.button:
            Button button = new Button(this);
            button.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + 9);
            break;

        case R.id.button2:
            Button button2 = new Button(this);
            button2.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + 8);
            break;

        case R.id.button3:
            Button button3 = new Button(this);
            button3.setBackgroundColor(Color.CYAN);
           textView.setText(textView.getText() +"" + 7);
            break;

        case R.id.button4:
            Button button4 = new Button(this);
            button4.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + 6);
            break;

        case R.id.button5:
            Button button5 = new Button(this);
            button5.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + 5);
            break;

        case R.id.button6:
            Button button6 = new Button(this);
            button6.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + 4);
            break;

        case R.id.button7:
            Button button7 = new Button(this);
            button7.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + 3);
            break;

        case R.id.button8:
            Button button8 = new Button(this);
            button8.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + 2);
            break;

        case R.id.button9:
            Button button9 = new Button(this);
            button9.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + 1);
            break;

        case R.id.button10:
            Button button10 = new Button(this);
            button10.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + 0);
            break;

        case R.id.button11:
            Button button11 = new Button(this);
            button11.setBackgroundColor(Color.CYAN);
            textView.setText(textView.getText() +"" + '.');
            break;



        case R.id.button13:
            x=Double.parseDouble((String)textView.getText());
            op=1;
            Button button13 = new Button(this);
            button13.setBackgroundColor(Color.CYAN);
            textView.setText(" ");
            break;

        case R.id.button14:
            x=Double.parseDouble((String)textView.getText());
            op=2;
            Button button14 = new Button(this);
            button14.setBackgroundColor(Color.CYAN);
            textView.setText(" ");
            break;

        case R.id.button15:
            x=Double.parseDouble((String)textView.getText());
            op=3;
            Button button15 = new Button(this);
            button15.setBackgroundColor(Color.CYAN);
            textView.setText(" ");
            break;

        case R.id.button16:
            x=Double.parseDouble((String)textView.getText());
            op=4;
            Button button16 = new Button(this);
            button16.setBackgroundColor(Color.CYAN);
            textView.setText(" ");
            break;
        case R.id.button12:
            y=Double.parseDouble((String) textView.getText());
            Button button12 = new Button(this);
            button12.setBackgroundColor(Color.CYAN);
            switch(op)
            {
                case 1:
                {
                    result=x+y;
                    break;
                }
                case 2:
                {
                    result=x-y;
                    break;
                }
                case 3:
                {
                    result=x*y;
                    break;
                }
                case 4:
                {
                    result=x/y;
                    break;
                }
                default :
                    break;
            }
            if(result-Math.floor(result)==0)
            {
                result=Math.floor(result);
            }
            textView.setText(result+"");
            break;

        default:
            break;
    }

}

}

这是xml文件

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

<Button
    android:text="@string/button0"
    android:id="@+id/button"
    android:textColor="#0A0A0A"
    android:textSize="25sp"

    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@drawable/buttonshape"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"

    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"
    android:clickable="true"
    android:contentDescription="@string/click1"
    android:contextClickable="true" />

<TextView
    android:layout_width="350dp"
    android:layout_height="100dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/text1"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="#ffffff" />

<Button
    android:textColor="#0A0A0A"
    android:textSize="25sp"

    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@drawable/buttonshape"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:text="@string/button"
    android:id="@+id/button2"
    android:layout_alignTop="@+id/button"
    android:layout_toRightOf="@+id/button"
    android:layout_toEndOf="@+id/button"
    android:layout_marginLeft="2.5dp"/>

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button7"
        android:id="@+id/button3"
        android:layout_alignTop="@+id/button13"
        android:layout_toRightOf="@+id/button5" />

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button6"
        android:id="@+id/button4"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"/>

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button5"
        android:id="@+id/button5"
        android:layout_alignTop="@+id/button4"
        android:layout_toRightOf="@+id/button4"
        android:layout_toEndOf="@+id/button4"
        android:layout_marginLeft="2.5dp"/>

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button4"
        android:id="@+id/button6"
        android:layout_alignTop="@+id/button5"
        android:layout_toRightOf="@+id/button5" />

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button3"
        android:id="@+id/button7"
        android:layout_below="@+id/button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"/>

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button10"
        android:id="@+id/button8"
        android:layout_below="@+id/button5"
        android:layout_alignLeft="@+id/button5"
        android:layout_alignStart="@+id/button5"
        android:layout_marginTop="10dp"/>

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button11"
        android:id="@+id/button9"
        android:layout_alignTop="@+id/button15"
        android:layout_toRightOf="@+id/button8" />

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button12"
        android:id="@+id/button10"
        android:layout_below="@+id/button9"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"/>

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button13"
        android:id="@+id/button11"
        android:layout_below="@+id/button9"
        android:layout_alignLeft="@+id/button8"
        android:layout_alignStart="@+id/button8"
        android:layout_marginTop="10dp"/>

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button18"
        android:id="@+id/button12"
        android:layout_alignTop="@+id/button16"
        android:layout_toRightOf="@+id/button11" />



    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"

        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button14"
        android:id="@+id/button13"
        android:layout_above="@+id/button4"
        android:layout_alignParentRight="true" />

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button15"
        android:id="@+id/button14"
        android:layout_alignTop="@+id/button6"
        android:layout_alignParentRight="true" />

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button16"
        android:id="@+id/button15"
        android:layout_alignTop="@+id/button8"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView" />

    <Button
        android:textColor="#0A0A0A"
        android:textSize="25sp"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:text="@string/button17"
        android:id="@+id/button16"
        android:layout_below="@+id/button9"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="10dp"/>

这是AndroidManifest文件:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

这是LogCat

12-21 22:15:23.620 1425-1425/com.example.kartikeya.calc D/dalvikvm: Not late-enabling CheckJNI (already on)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc D/AndroidRuntime: Shutting down VM
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb2fed180)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: FATAL EXCEPTION: main
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kartikeya.calc/com.example.kartikeya.calc.MainActivity}: java.lang.NullPointerException
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:  Caused by: java.lang.NullPointerException
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.view.View.<init>(View.java:2696)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.view.View.<init>(View.java:2744)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.widget.TextView.<init>(TextView.java:449)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc `E/AndroidRuntime:     at android.widget.TextView.<init>(TextView.java:442)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.widget.TextView.<init>(TextView.java:437)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at com.example.kartikeya.calc.MainActivity.<init>(MainActivity.java:14)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at java.lang.Class.newInstanceImpl(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at java.lang.Class.newInstance(Class.java:1319)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:123) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4424) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
12-21 22:15:24.140 1425-1431/com.example.kartikeya.calc I/dalvikvm: threadid=3: reacting to signal 3
12-21 22:15:24.140 1425-1431/com.example.kartikeya.calc I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt'
12-21 22:15:24.160 1425-1431/com.example.kartikeya.calc I/dalvikvm: threadid=3: reacting to signal 3
12-21 22:15:24.160 1425-1431/com.example.kartikeya.calc I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt'
12-21 22:15:27.801 1425-1425/? I/Process: Sending signal. PID: 1425 SIG: 9

1 个答案:

答案 0 :(得分:0)

您的问题就在这一行some_variable = '1.2.3.45-r67' // comment with word VERSION inside it

您正试图通过TextView textView = new TextView(this);方法在此视图上调用setText(),但此观点并未被夸大。在操作之前,您必须对视图进行充气,就像使用onClick

一样

因此,在您的Button button = (Button) findViewById(R.id.button);中,您希望放入onCreate()并删除textView = (TextView) findViewById(R.id.your_text_view_id);行 - 只需将其设为TextView textView = new TextView(this);