应用程序关闭未解决的安卓工作室

时间:2013-05-20 12:54:38

标签: android performance android-emulator

我目前正在研究华氏温度转换器Android应用程序。我尝试在模拟器上运行应用程序,每次它给出“你的应用程序突然停止”错误。尝试了很多东西,如增加模拟器的内存,重新启动。我还将活动添加到manifest.xml。

mainactivity.java代码

package com.example.yoyo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Menu;


public class MainActivity extends Activity {

    EditText input;
    EditText answer;
    TextView celsius;
    TextView fahrenheit;
    Button convert;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        this.findAllViewsById();
        convert.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
            String query = input.getText().toString();
            answer.setText(query);

            }
        });
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @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;
    }
    public void findAllViewsById()
    {

        input = (EditText) findViewById(R.id.Input);
        answer= (EditText) findViewById(R.id.Answer);
        convert= (Button) findViewById(R.id.ConvertButton);
        celsius= (TextView) findViewById(R.id.celsius);
        fahrenheit= (TextView) findViewById(R.id.fahrenheit);
               }

}

清单

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.yoyo.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>

</manifest>

1 个答案:

答案 0 :(得分:0)

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.findAllViewsById();
    convert.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
        String query = input.getText().toString();
        answer.setText(query);

        }
    });

}

首先super.onCreate必须是你的onCreate。如果你没有调用findViewById

,那么你不能做所有相关的setContentView操作