不幸的是[app]已停止 - 请帮助我解决身份不明的问题

时间:2014-03-01 01:59:02

标签: java android xml

这是我在Android应用中的第一次尝试。但是,当我尝试在模拟器中运行它时,我收到一个错误,指示应用程序在加载之前已停止。我已经倾注了资源而无法找到解决方案,所以我在这里发布代码,希望有人能够识别我的问题。如果你能帮助我,请提前多多谢谢!我会发布我无意中遗漏的任何内容。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.atillalabs.debatebreakrounds"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.debatebreakrounds.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

MainActivity.java

package com.atillalabs.debatebreakrounds;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import com.atillalabs.debatebreakrounds.R;

public class MainActivity extends Activity
{

    boolean continues;
    String prst;

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

        RelativeLayout container = (RelativeLayout)findViewById(R.id.maincontainer);
        Button calculate = (Button)container.findViewById(R.id.calculated);
        calculate.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                simulate();
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void simulate()
    {
        try
        {
            continues = true;

            EditText numbreak1 = (EditText)findViewById(R.id.editText1);  
            int numbreak = Integer.parseInt(numbreak1.getText().toString());
            EditText numrds1 = (EditText)findViewById(R.id.editText2);  
            int numrds = Integer.parseInt(numrds1.getText().toString());
            EditText pmrds1 = (EditText)findViewById(R.id.editText3);  
            int pmrds = Integer.parseInt(pmrds1.getText().toString());
            EditText numrdsknown1 = (EditText)findViewById(R.id.editText4); 
            int numrdsknown = Integer.parseInt(numrdsknown1.getText().toString());
            EditText numexpectedwin1 = (EditText)findViewById(R.id.editText5);  
            int numexpectedwin = Integer.parseInt(numexpectedwin1.getText().toString());
            int presentwinct;

            int[] knownrslts;
            ArrayList<Integer> inp = new ArrayList<Integer>();

            for (int i = 0; i <= numrdsknown; i++)
            {
                if (numrdsknown == 0)
                    inp.add(Integer.parseInt(promptBox("How Many Entries", "How many entries are there?")));
                else
                    inp.add(Integer.parseInt(promptBox("Teams by Record", "How many teams have a " + (numrdsknown-i) + "-" + i + " record?")));
                if (inp.get(inp.size()-1) < 0) throw new Exception("Input may not be negative");
                if (!continues) return;
            }

            if (numrdsknown > 0)
                presentwinct = Integer.parseInt(promptBox("Your record", "How many of " + numrdsknown + " known rounds have you won?"));
            else
                presentwinct = 0;

            if (!continues) return;

            knownrslts = new int[inp.size()];
            for (int j = 0; j < inp.size(); j++)
                knownrslts[j] = inp.remove(0);

            Breakcalc calulator = new Breakcalc();

            double probabilityOfBreakingGivenCurrentPerformance = 
                    calulator.probabilityOfBreakingGivenCurrentPerformance(numbreak, presentwinct, numrds, pmrds, knownrslts);
            double expectedNumFinalWinsGivenCurrentPerformance = 
                    calulator.expectedNumFinalWinsGivenCurrentPerformance(numbreak, presentwinct, numrds, pmrds, knownrslts);
            double probabilityOfBreakingGivenExpectedFinalPerformance = 
                    calulator.probabilityOfBreakingGivenExpectedFinalPerformance(numbreak, numexpectedwin, numrds, pmrds, knownrslts);


            // temp - show results
            messageBox(probabilityOfBreakingGivenCurrentPerformance + " " + expectedNumFinalWinsGivenCurrentPerformance + " " + probabilityOfBreakingGivenExpectedFinalPerformance);
        }
        catch (Exception e)
        {
            messageBox("The numbers you entered do not add up.");
        }
    }

    public void nocontinue()
    {
        continues = false;
    }

    public String promptBox(String title, String text)
    {
        prst = new String();
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle(title);
        alert.setMessage(text);
        final EditText input = new EditText(this);
        alert.setView(input);
        alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString();
            setpromptretstr(value);     
         }
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int whichButton) {
             nocontinue();
        }
        });
        alert.show();
        return prst;
    }

    public void setpromptretstr(String x)
    {
        prst = x;
    }

    public void messageBox(String message)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message)
               .setCancelable(false)
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        //do nothing
                   }
               });
        AlertDialog alert = builder.create();
        alert.show();

}

}

activity_main.xml中

    <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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:id="@+id/maincontainer">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="17dp"
        android:text="Number that will break:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="15dp"
        android:text="Number of rounds with known results:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentRight="true"
        android:ems="2"
        android:inputType="number" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:ems="2"
        android:inputType="number" >

        <requestFocus />

        <requestFocus />

    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@+id/textView2"
        android:ems="2"
        android:inputType="number" >
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="14dp"
        android:text="Total number of rounds:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="14dp"
        android:text="Number of power-matched rounds:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignParentRight="true"
        android:ems="2"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/editText4"
        android:layout_marginTop="15dp"
        android:text="Number of rounds you expect to win:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/editText5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignParentRight="true"
        android:ems="2"
        android:inputType="number" />

    <Button
        android:id="@+id/calculated"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_alignRight="@+id/editText5"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="14dp"
        android:text="Simulate Tournament" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

使用相同的包名称来注册AndroidManifest.xml中的MainActivity Activity。改为:

<activity
            android:name="com.atillalabs.debatebreakrounds.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">
....