Android 更改按钮和文本视图的顺序会使我的应用程序崩溃

Android 更改按钮和文本视图的顺序会使我的应用程序崩溃,android,android-layout,button,textview,Android,Android Layout,Button,Textview,我是Android开发新手,我有一个奇怪的问题。。根据我的布局顺序,应用程序可以完美运行,或者在打开前崩溃 我的java代码: package com.exmple.helloandroid; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.Toast; import android.view.View; public clas

我是Android开发新手,我有一个奇怪的问题。。根据我的布局顺序,应用程序可以完美运行,或者在打开前崩溃

我的java代码:

package com.exmple.helloandroid;

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

public class HelloAndroid extends Activity{
/** Called when the activity is first created. */

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

            // According to logcat. Below is the error line
    Button myButton = (Button) findViewById(R.id.button1);
    myButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
                        Toast.makeText(HelloAndroid.this, "ImageButton clicked!", Toast.LENGTH_SHORT).show();
                }
    });
}
}
以及布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text1" /> 

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text1" />
</LinearLayout>

我运行你的代码,你是正确的,我给出了一个例外,如果我们在按钮之前移动文本视图。这很奇怪。但我发现这实际上是一个身份问题。当我将TextView移动到up并开始工作后更改Id时。但我不知道为什么

也许有人知道。但您也可以尝试通过链接视图的id

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/main_id_text_my"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text2" />

    <Button
        android:id="@+id/main_id_btn_my"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text1" />

</LinearLayout>

另外,别忘了清理和构建您的项目

我运行了您的代码,您是对的,我给出了一个例外,我们将文本视图移到按钮之前。这很奇怪。但我发现这实际上是一个身份问题。当我将TextView移动到up并开始工作后更改Id时。但我不知道为什么

也许有人知道。但您也可以尝试通过链接视图的id

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/main_id_text_my"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text2" />

    <Button
        android:id="@+id/main_id_btn_my"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text1" />

</LinearLayout>
另外,不要忘记清理和构建项目

移动TextView位置时需要小心。如果将TextView移动到其他地方后不更改其ID,则可能会导致应用程序崩溃

我不知道为什么会发生这种情况,但更改id可以解决此崩溃问题。

移动TextView的位置时需要小心。如果将TextView移动到其他地方后不更改其ID,则可能会导致应用程序崩溃


我不知道为什么会发生这种情况,但更改id可以解决此崩溃问题。

清理项目将解决问题。

清理项目将解决问题。

清理整个项目并重新构建!希望它能解决问题,请分享你的日志。当应用程序崩溃时。嗯,清理和重建工作!清理整个项目并重新构建!希望它能解决问题,请分享你的日志。当应用程序崩溃时。嗯,清理和重建工作!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/main_id_text_my"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text2" />

    <Button
        android:id="@+id/main_id_btn_my"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text1" />

</LinearLayout>
Button myButton = (Button) findViewById(R.id.main_id_btn_my);
        myButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                            Toast.makeText(TestAndActivity.this, "ImageButton clicked!", Toast.LENGTH_SHORT).show();
                    }
        });