0

我目前正在开发一个华氏到摄氏度转换器的安卓应用程序。我尝试在模拟器上运行该应用程序,每次它都会出现“您的应用程序突然停止”错误。尝试了很多事情,比如增加模拟器的内存,重新启动。我还将活动添加到 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>
4

1 回答 1

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

于 2013-05-20T12:55:40.900 回答