Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扩展自定义布局类-从未使用构造函数_Java_Android_Xml_Android Layout_View - Fatal编程技术网

Java 扩展自定义布局类-从未使用构造函数

Java 扩展自定义布局类-从未使用构造函数,java,android,xml,android-layout,view,Java,Android,Xml,Android Layout,View,我正在使用给定的Web视图 我知道主要活动等工作正常,因为我在用XML实现框架布局之前对其进行了测试。但是现在我使用框架布局,我遇到了一些问题 我将向您提供我的代码和快速解释: XML <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">

我正在使用给定的Web视图

我知道主要活动等工作正常,因为我在用XML实现框架布局之前对其进行了测试。但是现在我使用框架布局,我遇到了一些问题

我将向您提供我的代码和快速解释:

XML

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

    <com.example.dencallanan.giftestapp.GifWebView
        android:id="@+id/GWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />

    <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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_marginBottom="153dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:hint="USERNAME" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_marginTop="58dp"
            android:layout_alignTop="@+id/editText"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignLeft="@+id/editText"
            android:layout_alignStart="@+id/editText"
            android:hint="PASSWORD" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Log In"
            android:id="@+id/button"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp" />
    </RelativeLayout>


</FrameLayout>
我想我的问题就在这里。我在网上看到,我应该添加上面看到的第二个两个构造函数方法。但第一个构造函数似乎是唯一被使用的构造函数,也就是说,第二个构造函数给出了从未使用过的方法的警告

也许我错了,但假设您需要这些构造函数来实现XML中的布局类,而这些构造函数从未被使用过。。。为什么?

以下两行是我的主要活动,旨在帮助您了解发生了什么

    gWView = (GifWebView) findViewById(R.id.GWebView);
    gWView = new GifWebView(this, "file:///android_asset/watg.gif");
我真的很感激任何帮助,这个问题已经困扰我好几个小时了

问题

Android屏幕上没有显示任何内容。。。一片空白

编辑黑带

活动\u main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.example.dencallanan.giftestapp.GifWebView
        android:id="@+id/GWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        myapp:fpath="watg"  <!-- also tried "watg.gif" -->

        />

    <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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_marginBottom="153dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:hint="USERNAME" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_marginTop="58dp"
            android:layout_alignTop="@+id/editText"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignLeft="@+id/editText"
            android:layout_alignStart="@+id/editText"
            android:hint="PASSWORD" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Log In"
            android:id="@+id/button"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp" />
    </RelativeLayout>


</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="GifWebView">
        <attr name="fpath" format="string" />
    </declare-styleable>
</resources>

您不应该重载视图的构造函数。使用

 public GifWebView(Context context, AttributeSet attrs) {
        super(context, attrs); 
 }


并且不以编程方式提供默认参数,也不为您的自定义
视图定义自定义属性。谢谢您的回答,您能否对此进行一点扩展,假设我知道的很少,因为我只是一个初学者。“非默认参数”或“我的自定义
视图的自定义属性”是什么意思?这能解决我的问题吗?还是只是一个好习惯?应该使用那些构造函数方法吗?为什么不是我的?好吧,我想我明白你的意思了。因此,我在values文件夹中创建了一个
attrs.xml
文件。然后我在里面创建了一个新的布局。我在主xml文件中使用此布局,可以添加样式和自定义属性。然后我从类中访问这些属性。我有两个问题。如何指定新布局对应于Web视图?我要保留所有3个构造函数,并将
loadURL()
方法放在所有构造函数中。非常感谢你对我的耐心,“如果这能奏效,我会非常高兴的。”哦,我想出了第一个问题,但不知道第二个问题。我是否保留了所有3个构造函数,并将loadURL()方法放在所有构造函数中?谢谢,我似乎不知道用什么替换“myapp”。
public class GifWebView extends WebView {

    public GifWebView(Context context) {

        super(context);
    }

    public GifWebView(Context context, AttributeSet attrs) {

        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.GifWebView);
        CharSequence s = a.getString(R.styleable.GifWebView_fpath);
        loadUrl(s.toString());
    }

    public GifWebView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.GifWebView);
        CharSequence s = a.getString(R.styleable.GifWebView_fpath);
        loadUrl(s.toString());
    }
}    
 public GifWebView(Context context, AttributeSet attrs) {
        super(context, attrs); 
 }
public GifWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
}