简体   繁体   English

android studio在简单请求时崩溃

[英]android studio crashing on simple request

I am new to android studio and I have tried to write a simple program where the text "hello world" appears on the screen when a button is clicked, however, every time the button is clicked the program crashes. 我是android studio的新手,我尝试编写一个简单的程序,当单击按钮时,屏幕上会显示文本“ hello world”,但是,每次单击该按钮时,程序都会崩溃。

activity_main.xml : activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<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="com.example.numsm_000.helloworld.MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me!"
        android:onClick="showMessage"
        android:id="@+id/button"
        android:layout_marginLeft="200dp"/>

</RelativeLayout>

MainActivity.java : MainActivity.java:

   package com.example.numsm_000.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    }

    public void showMessage(){
        TextView textView = (TextView) findViewById(R.id.text);
        textView.setText(R.string.app_name);
    }

}

Pass View as a parameter to showMessage() because onClick will look for a method which has View parameter View作为参数传递给showMessage()因为onClick会查找具有View参数的方法

so it become 所以变成

public void showMessage(View v){
    TextView textView = (TextView) findViewById(R.id.text);
    textView.setText(R.string.app_name);
}

Hope it helps :) 希望能帮助到你 :)

use following code 


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 TextView textView;
Button btnClickMe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text);
      btnClickMe=(Button)findViewById(R.id.button);
       btnClickMe.setOnClickListener(this);

    }


 @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
        textView.setText(R.string.app_name);
              break;

}

Try the following... 尝试以下...

TextView textView = (TextView) findViewById(R.id.text);

Cut this line of code from showMessage() and put it in onCreate()... or you could just specify View view in ur method .. 从showMessage()剪切掉这行代码,并将其放入onCreate()...,或者您可以只在ur方法中指定View视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM