不幸的是你好已经停止了 [英] Unfortunately hello has stopped

查看:84
本文介绍了不幸的是你好已经停止了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,我有一个问题,就是我每次尝试在模拟器上运行一个android代码时都会出现一条错误消息,我不知道该怎么做,不幸的是你好已停止了(你好是的名字是应用程序),它包含一个按钮,当单击时,textview将填充世界问候语。使用的代码如下



Hello everyone, I have a problem that is every time I try to run an android code on the emulator an error message appears and i don't know what to do which is Unfortunately hello has stopped (hello is the name of the application) which consists of a button that when clicked a textview is filled with the world hello. the code used is as follows

package com.example.hello;

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

public class MainActivity extends Activity {
    Button btn = (Button) findViewById(R.id.button1);
    TextView tv = (TextView)findViewById(R.id.textView1);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                tv.setText("Hello Welcome");	
            }
        });
    }
    
    @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;
    }
}





电脑是奔腾双核2GB Ram 1.65可用32位操作系统windows 7和模拟器是Android 4.4 Api Level 19 512 Ram 16堆和200内部存储



The computer is Pentium Dual core 2GB Ram 1.65 usable 32 bit operating System windows 7 and the emulator is Android 4.4 Api Level 19 512 Ram 16 heap and 200 internal storage

推荐答案

你必须在onCreate
也可以在内部类中使用它时将最终添加到电视中

You have to place the btn and tv initialization after the setContentView inside the onCreate
also to add final to the tv as you are using it in an inner class
setContentView(R.layout.activity_main);
		
Button btn = (Button) findViewById(R.id.button1);
final TextView tv = (TextView)findViewById(R.id.textView1);



结束。


The End.


这篇关于不幸的是你好已经停止了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆