2014-05-11 18 views
6

Ich versuche, Code in Android zu schreiben, um Parameter in einem AttributeSet aus attrs.xml-Datei festzulegen. Aber ich erhalte den Fehler "Ressource nicht gefunden".Wie erstelle ich ein AttributSet in Android?

Java-Code

MainActivity.java

package com.example.mycompoundbutton; 

import org.xmlpull.v1.XmlPullParser; 
import android.os.Bundle; 
import android.util.AttributeSet; 
import android.util.Xml; 
import android.app.Activity; 
import android.content.res.Resources; 


public class MainActivity extends Activity 
{ 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    Resources res = this.getResources(); 

    XmlPullParser parser = res.getXml(R.attr.xyz); 

    AttributeSet attrs = Xml.asAttributeSet(parser); 

    MyCompound my = new MyCompound(this,attrs); 

    my.MyTestFun(300,500); 

} 
} 

MyCompound.java

package com.example.mycompoundbutton; 


import android.content.Context; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.widget.CompoundButton; 

public class MyCompound extends CompoundButton 
{ 

public MyCompound(Context context, AttributeSet attrs) 
{ 

     super(context, attrs); 

     TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyCustomView, R.attr.xyz, 0); 

     a.recycle(); 

} 

public void MyTestFun(int x,int y) 
{ 

    // Some Code to Execute 


}} 

attrs.xml

<resources> 
<declare-styleable name="MyCustomView"> 

    <attr name="abc" format="integer"/>    
    <attr name="pqr" format="integer" /> 

</declare-styleable> 

<attr name="xyz" format="integer"/> 

</resources> 

Fehler:

05-11 07:29:27.345: E/AndroidRuntime(1919): FATAL EXCEPTION: main 
05-11 07:29:27.345: E/AndroidRuntime(1919): java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.example.mycompoundbutton/com.example.mycompoundbutton.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f010000 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.os.Looper.loop(Looper.java:137) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at java.lang.reflect.Method.invoke(Method.java:525) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at dalvik.system.NativeStart.main(Native Method) 
05-11 07:29:27.345: E/AndroidRuntime(1919): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f010000 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.content.res.Resources.getValue(Resources.java:1118) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2304) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.content.res.Resources.getXml(Resources.java:983) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at com.example.mycompoundbutton.MainActivity.onCreate(MainActivity.java:24) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.app.Activity.performCreate(Activity.java:5133) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
05-11 07:29:27.345: E/AndroidRuntime(1919):  ... 11 more 

So, hier ist alles oben-Code und Fehlerbeschreibung. Ich möchte MyCompound Klasse von MainActivity anrufen. Ich weiß, wie es zu tun, um eine XML Datei wie

<com.example.mycompoundbutton.MyCompound 
    ... attributes here ... 
> 
</com.example.mycompoundbutton.MyCompound> 

Diese obige Struktur wird mir helfen, ein statisches benutzerdefiniertes Layout zu entwerfen, aber es wird mir nicht ein dynamisches Layout zu entwerfen helfen. Wie kann ich die MyCompound Klasse von MainActivity mit einem AttributeSet anrufen?

+0

'com.example.mycompoundbutton.MainActivity.onCreate (MainActivity.java:24)' Was ist Zeile 24 von 'MainActivity.java'? –

Antwort

1

Nach dem erneuten Lesen wird eines Ihrer Probleme darin liegen, dass das Attribut xyz außerhalb von CustomViewStyleable in attrs.xml deklariert wird.


Statt Ihre benutzerdefinierte Ansicht Konstruktor direkt aufzurufen, können Sie das System lassen damit umgehen, indem Sie Ihre benutzerdefinierte Ansicht von XML-aufpumpen.

Erstellen Sie eine XML-Layoutdatei, die nur Ihre benutzerdefinierte Ansicht enthält, z. view_my_compound.xml:

// the view which will contain your MyCompoundButton 
View container = findViewById(R.id.XXXXXXXX); 
LayoutInflater inflater = getLayoutInflater(); 
MyCompound button = (MyCompound) inflater.inflate(R.layout.view_my_compound, container, true); 

Auf diese Weise brauchen Sie nicht das Attribut außer im View-Klasse festgelegt zu betrachten;:

<?xml version="1.0" encoding="utf-8"?> 
<com.example.mycompoundbutton.MyCompound 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:xyz="2" /> 

Dann in Ihrer Aktivität, werden Sie es nur aufblasen Android ruft den Konstruktor MyCompound(Context, AttributeSet) mit den in der XML-Layoutdatei festgelegten Werten für Sie auf.

Wenn Sie XYZ-Parameter programmgesteuert festlegen müssen, stellen Sie eine öffentliche Methode in MyCompound bereit, mit der Sie sie festlegen können.

+0

Kein Freund bekommen Fehler ... Die Methode von (Context) im Typ LayoutInflater ist nicht anwendbar für die Argumente (MainActivity, View, int) – MyProg

+0

sorry, 'LayoutInflater.from (this) .inflate (R.layout.view_my_compound, Container, wahr); 'Aber auch die erste geänderte Zeile dieser Antwort. – ataulm

0

Guy Ich denke, Ihr Problem zu hören ist

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyCustomView, R.attr.xyz, 0); 

"R.attr.xyz" ist NOTFOUND. Sie können sehen, diese click Hear

0

Ich schlage vor, einen <com.example.mycompoundbutton.MyCompound> Tag in activity_main.xml mit anstatt zu versuchen, eine Instanz in Ihrem Java-Code zu erstellen. Dadurch kann Android Ihre Ansicht aufblasen, wenn Sie setContentView() anrufen, ohne selbst mit einem Inflator zu spielen. Sie können alle Standardattribute auf dieses Tag anwenden, einschließlich android:id. Dann können Sie einfach findViewById() aufrufen, um einen Verweis auf die MyCompound Ansicht zu erhalten, genau wie bei jeder Standardansicht.

Verwandte Themen