我的Android应用程序意外关闭。 Eclipse说它无法连接到本地主机

时间:2011-03-14 22:18:46

标签: java android linux eclipse

我正在创建一个Android应用程序。编译得很好,eclipse没有发现任何错误。但是当我运行它时,我会在堆栈跟踪中得到它。

<disconnected>DalvikVM[localhost:8639]  

这是我的代码:

package com.warhammerdicerrolleralpha;


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.ImageView;
import android.widget.TextView;

public class myMain extends Activity 
{

    EditText enternumberofdice;

    TextView textGenerateNumber;

    int number1;

    int number2;

    ImageView i;

    private EditText text, text2, text3;

    private Button btutorial1;

    private int myFaceValue;

    int myNum;

    /**
     * Called when the activity is first created.
     * 
     * @return
     */
    @Override

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        textGenerateNumber = (TextView) findViewById(R.id.text4);

        number1 = Integer.parseInt(text.getText().toString());

        number2 = Integer.parseInt(text2.getText().toString());

        ImageView i = new ImageView(this);

        i.setAdjustViewBounds(true);

    }

    public void go() 
    {
        while (myNum > 0) 
        {

            textGenerateNumber.setText(String.valueOf(enternumberofdice));

             --myNum;

            return;
        }
    }

    public int roll() 
    {
        int val = (int) (6 * Math.random() + 1); // Range 1-6
        setValue(val);
        return val;
    }

    {
        try 
        {
            myNum = Integer.parseInt(enternumberofdice.getText().toString());
        } 
            catch (NumberFormatException nfe) 
        {
            enternumberofdice.setText("Does not work");
        }
    }

    public int getValue() 
    {
        return myFaceValue;
    }

    public void setValue(int myFaceValue) 
    {
        this.myFaceValue = myFaceValue;

    }

    {
        switch (myFaceValue) 
        {
        case 5:
            i.setImageResource(R.drawable.dicefive);
            break;
        case 1:
            i.setImageResource(R.drawable.diceone);
            break;
        case 3:
            i.setImageResource(R.drawable.dicethree);
            break;
        case 2:
            i.setImageResource(R.drawable.dicetwo);
            break;
        case 4:
            i.setImageResource(R.drawable.dicefour);
            break;
        case 6:
            i.setImageResource(R.drawable.dicesix);
            break;
        default:
            i.setImageResource(R.drawable.error);
            break;
        }

        text = (EditText) findViewById(R.id.editText1);

        text2 =(EditText) findViewById(R.id.editText2);

        text3 = (EditText) findViewById(R.id.editText3);  

        btutorial1 = (Button) findViewById(R.id.button1);

        btutorial1.setOnClickListener((OnClickListener) this);

        Button buttonGenerate = (Button) findViewById(R.id.button1);

        enternumberofdice = (EditText) findViewById(R.id.enternumberofdice);

        Button buttonGenerate2 = (Button) findViewById(R.id.battlecalculate);

        buttonGenerate2.setOnClickListener(new OnClickListener()    
        {

            @Override
            public void onClick(View v) 
            {
                    setContentView(R.layout.main2);
            }

        });

        buttonGenerate.setOnClickListener(new OnClickListener()     
        {
            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                go();

                roll();
            }

        });
    }

    public void onClick(View view) 
    {
        switch (view.getId()) 
        {
            case R.id.button1:

                if (number1 > number2) 
                {
                    text3.setText("Three and above");       
                    return;
                }

                else if (number1 < number2) 
                {
                    text3.setText("Five and above");        
                    return;
                }

                else if (number1 == number2) 
                {
                    text3.setText("Four and above");        
                    return;
                }

                else
                {
                    text3.setText("Not Working");       
                    return;
                }

        }
    }
}

P.S。如何在帖子中添加xml代码。当我将xml代码添加到我的帖子时,它不会出现在预览页面中。

这是我的堆栈跟踪。

DalvikVM [本地主机:8695]
    线程[&lt; 1&gt; main](Suspended(例外RuntimeException))
        ActivityThread.performLaunchActivity(ActivityThread $ ActivityRecord,Intent)行:2585
        ActivityThread.handleLaunchActivity(ActivityThread $ ActivityRecord,Intent)行:2679
        ActivityThread.access $ 2300(ActivityThread,ActivityThread $ ActivityRecord,Intent)行:125         ActivityThread $ H.handleMessage(消息)行:2033
        ActivityThread $ H(Handler).dispatchMessage(Message)行:99         Looper.loop()行:123         ActivityThread.main(String [])行:4627
        Method.invokeNative(Object,Object [],Class,Class [],Class,int,boolean)line:not available [native method]
        Method.invoke(Object,Object ...)行:521
        ZygoteInit $ MethodAndArgsCaller.run()行:868
        ZygoteInit.main(String [])行:626         NativeStart.main(String [])行:不可用[native method]
    线程[&lt; 6&gt; Binder Thread#2](正在运行)     线程[&lt; 5&gt; Binder Thread#1](正在运行)

2 个答案:

答案 0 :(得分:0)

你做的类级变量初始化,例如textGenerateNumber,必须在调用setContentView方法中的onCreate之后完成。

更新:重构代码应该是这样的。但是,number1number2分配没有意义,因为您尚未初始化texttext2变量。

....

EditText enternumberofdice;
TextView textGenerateNumber;
private EditText text, text2, text3;
private Button btutorial1;
int number1;
int number2;
ImageView i;
private int myFaceValue;
int myNum;


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

    textGenerateNumber = (TextView) findViewById(R.id.text4);

    //BUG: will fail since text/text2 isn't initialized
    //number1 = Integer.parseInt(text.getText().toString());
    //number2 = Integer.parseInt(text2.getText().toString());

    i = new ImageView(this);
    i.setAdjustViewBounds(true);
}

答案 1 :(得分:0)

final TextView textGenerateNumber = (TextView) findViewById(R.id.text4);

onCreate方法之前运行,因此您的观点尚未存在。在致电setContentView()后,添加所有代码以检索视图。