java - 错误 :(23, 26) 不允许字符串类型(位于 'src' ,值为 'pen' )。

标签 java android xml android-layout android-studio

所以我正在尝试学习 Android 开发,我正在尝试这个程序,但它给了我错误 错误:(23, 26) 不允许字符串类型(在“src”处,值为“pen”)。 当我尝试在 Nexus 6 上运行它时。

Java 代码: 导入 android.support.v7.app.AppCompatActivity; 导入 android.os.Bundle; 导入 android.widget.Button; 导入 android.widget.TextView; 导入 android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

        Button ImageButton = (Button)findViewById(R.id.Image);

        ImageButton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v)
                    {
                        TextView NL = (TextView)findViewById(R.id.Asd)
                        NL.setText("AAA");
                    }
                }
        );


    }
}

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="com.example.bassammetwally.anew.MainActivity">
    <RelativeLayout
        android:layout_width="400dp"
        android:layout_height="50dp"
        android:background="#3955a1">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Learning program"
            android:textColor="#ffffff"
            android:textSize="30dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="8dp"
            android:id="@+id/Asd"/>
        <ImageButton
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="pen"
            android:layout_marginLeft="330dp"
            android:layout_marginTop="5dp"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:id="@+id/image"/>
        </RelativeLayout>
</LinearLayout>

ManiFest:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.A.anew">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

最佳答案

错误消息清楚地说明了您的错误。您必须设置对 imageView 的可绘制引用。但你设置了一个字符串:

android:src="pen"

如果您有一个名为 pen 的可绘制(图像),请将该行更改为:

android:src="@drawable/pen"

关于java - 错误 :(23, 26) 不允许字符串类型(位于 'src' ,值为 'pen' )。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809272/

相关文章:

java - 为什么这个 xslt 转换会替换所有出现的情况?

java - 有效地搜索连续日期段的列表

Java EE 6、5、7 |托管 bean 或类列表 : EJBs , JSF bean 和..?

javascript - 如何将我的 Html/JS/CSS 知识用于 iO、Android 等?

python - 使用 Python 的 xml.dom.minidom

xml - 如何添加具有新 ID 的第二个元素?

java - 缓慢的递归函数。我怎样才能加快速度?

Java android 以自定义分辨率捕获照片

android - 从 ListView 编辑/删除项目的正确方法是什么

android - 如何为android中的所有分辨率设置用户界面