【问题标题】:Simple Android Button Malfunctioning简单的 Android 按钮故障
【发布时间】:2023-03-31 07:56:01
【问题描述】:

我有一个简单的 Android 应用程序,当我单击一个按钮时,它应该将文本更改为“已单击”。但是,当我运行代码并尝试单击按钮时,按钮会发出蓝色光,但没有其他反应。主要方法和对应的xml如下所示。感谢您的帮助!

package com.example.fortesting;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

Button button;
TextView tv1;

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    addListenerOnButton();

}

public void addListenerOnButton() {

    button = (Button) findViewById(R.id.btn_bt);
    tv1 = (TextView) findViewById(R.id.textView1);
    //tv1.setText("Well, this works");;

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
                tv1.setText("Clicked!");
        }

    });
    button.setClickable(true);

}




@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.main, container,
                false);
        return rootView;
    }
}

}

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/container">


        <Button
            android:id="@+id/btn_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="45dp"
            android:text="Click Me!" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_bt"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="69dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

【问题讨论】:

    标签: java android xml button onclicklistener


    【解决方案1】:

    当您声明 tv1 时,您必须将其声明为 final 以避免在 OnClickListener 中访问它时出现此错误:

    不能在不同方法中定义的内部类中引用非最终变量 tv1

    另外,将 addListenerOnButton() 方法调用移到 SupportFragmentManager 块上方:

    ...
    final TextView tv1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        addListenerOnButton();
    
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }
    ...
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      相关资源
      最近更新 更多