CardView

K

Kustaf

Neues Mitglied
0
Hallo, meinProblem ist folgendes: Ich möchte Cardviews im Javacode hinzufügen. Hierzu habe ich eine CardView Klasse erstellt().In meiner Hauptactivity möchte ich den Konstruktor aufrufen. Leider bekomme ich es nicht hin, da ich nicht weis was ich als Kontext übergeben muss. Hoffe hier kann mir jemand helfen.

MainActivity:
package com.example.versuch_1;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.CardView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

public class MainActivity extends ActionBarActivity {

public LinearLayout LinearLayout1;
public CardView CardView1;
public Context context1;

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

LinearLayout1 = (LinearLayout) findViewById(R.id.LinearLayout1);
context1 = this;
CardView1 = new CardViewe();
LinearLayout1.addView(CardView1);
}


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


CardViewe:
package com.example.versuch_1;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;

public class CardViewe extends CardView {





public CardViewe(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
public void setCardElevation(float radius) {
// TODO Auto-generated method stub
radius=2;
super.setCardElevation(radius);
}

@Override
public void setRadius(float radius) {
// TODO Auto-generated method stub
radius=2;
super.setRadius(radius);
}
}

Activity_Main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</LinearLayout>

 
versuchs mal mit dem button für PHP Code oder den nromalen Code nächstes mal ist angenehmer den Code zu studieren dann

PHP:
package com.example.versuch_1;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.CardView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

public class MainActivity extends ActionBarActivity {

public LinearLayout LinearLayout1;
public CardView CardView1;
public Context context1;

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

LinearLayout1 = (LinearLayout) findViewById(R.id.LinearLayout1);
context1 = this;
CardView1 = new CardViewe();
LinearLayout1.addView(CardView1);
}


@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);
}
}
CardViewe:

PHP:
package com.example.versuch_1;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;

public class CardViewe extends CardView {





public CardViewe(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
public void setCardElevation(float radius) {
// TODO Auto-generated method stub
radius=2;
super.setCardElevation(radius);
}

@Override
public void setRadius(float radius) {
// TODO Auto-generated method stub
radius=2;
super.setRadius(radius);
}
}
Activity_Main:
PHP:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</LinearLayout>
 
Werde ich machen, aber eine Lösung weist du auch nicht oder ?
 
du übergibst den context...brauchst auch nicht extra ein Context object zu erstellen einfach mit this das cardview alloziieren

Der ursprüngliche Beitrag von 17:15 Uhr wurde um 17:16 Uhr ergänzt:

-> new CradViewe(this);
 
Jaiel schrieb:
du übergibst den context...brauchst auch nicht extra ein Context object zu erstellen einfach mit this das cardview alloziieren

Der ursprüngliche Beitrag von 17:15 Uhr wurde um 17:16 Uhr ergänzt:

-> new CradViewe(this);

Das habe ich schon versucht, leider funktioniert das nicht.

Dann kommt: " Java.lang.NoClassDefFoundError: Failed resolution oft: Landroid/support/v7/cardview/R$styleable "
 
Zuletzt bearbeitet:
versuche mal die v4 library mit reinzunehmen(du hast die v7 hoffentlich auch geaddet zum buildpath oder?)
 
zumafx schrieb:
für Code sollte man die Code-tags (#) benutzen, für php die php-tags (php) :p

PHP tag=schöne farbige buchstaben :drool:= gut :flapper:
 
Jaiel schrieb:
versuche mal die v4 library mit reinzunehmen(du hast die v7 hoffentlich auch geaddet zum buildpath oder?)

Hatte beide im Projekt, daran kanns deshalb nicht liegen.
 
Ich finde einfach das Problem nicht. Ich hab jetzt alles noch mal durchgeschaut.
 
Hier mal das Projekt. Könntest du das zur Sicherheit auch noch einmal überprüfen ?
 

Anhänge

  • Versuch.zip
    3,2 MB · Aufrufe: 73
so wie ich das lese sollte
Code:
public CardView CardView1;
doch eher
Code:
public [B]CardViewe[/B] CardView1;
sein, oder ?
und dein
Code:
CardView1 = new CardViewe();
sollte ja einen Context übergeben kriegen laut deiner Cardviewe-klasse
Code:
CardView1 = new CardViewe(this);
das
Code:
context1 = this;
ist doppeltgemoppelt ;)

Hast du meinen Link zu stackoverflow gelesen ?
Zitat:
java.lang.NoClassDefFoundError
This exception indicates that the JVM looked in its internal
class definition data structure for the definition of a class and did not find it.

und ...

The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure
in the static initialization block) or any number of other problems.

Da kannst du ja mal ansetzen, aus irgendeinem Grund wird deine Class scheinbar nicht gefunden.
 
kustaf geh doch einach unter eclipse mit rechtsklick auf dein projekt ganz unten in ->"Properties" auf ->"Java BuildPath" und zum Reiter ->"Libraries"

ist dort die library v7 und v4 eingefügt? prüfe ob alles richtig ist
es scheint zur compile zeit da zu sein aber nciht zur runtime


du hast da bin ich mir fast sicher die benötigten .jars nciht eingefügt
 
Zuletzt bearbeitet:
Also das ändern von " public CardView CardView1;" hat nichts gebracht.
In meinem Projekt sind folgende JARs:-android-support-v7-cardview.jar
-v4.jar
- dazu noch die standard von Android die immer dabei sind
 
Zuletzt bearbeitet:
Brauch ich noch welche außer den genannten ?

Der ursprüngliche Beitrag von 19:41 Uhr wurde um 19:47 Uhr ergänzt:

hab noch mal bisschen was verändert und jetzt kommt eine neue Fehlermeldung.

04-12 17:45:56.997: E/AndroidRuntime(2242): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.versuch/com.example.versuch.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
 
werden die libs ind einem "libs" ordner angezeigt?
 

Ähnliche Themen

F
  • Feuerstern
Antworten
3
Aufrufe
744
Jaiel
Jaiel
B
Antworten
8
Aufrufe
905
Binbose
B
Zurück
Oben Unten