Autore Topic: java.lang.NoSuchMethodException con View personalizzata  (Letto 656 volte)

Offline BraganKimaru

  • Nuovo arrivato
  • *
  • Post: 3
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    samsung galaxy s4
  • Sistema operativo:
    windows 7
java.lang.NoSuchMethodException con View personalizzata
« il: 24 Ottobre 2013, 21:40:11 CEST »
0
salve, ho cominciato da poco a imparare android e volevo provare a fare qualcosa di semplice per la grafica, ho trovato e copiato questo codice e volevo intanto farlo funzionare così com'è per poi magari usarlo come base per altre applicazioni, ma mi dà questo problema NoSuchMethodException, ho cercato in giro e dicevano che è un problema con il costruttore della classe CustomView, ho provato come si vede nel file ad estendere tuti e tre i costruttori ma l'errore è sempre lo stesso, qualcuno può darmi una mano?



package com.provegrafica3;


import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.logging.Handler;

public class MainActivity extends ActionBarActivity {
    CustomView cv;
    Handler updateCvHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        cv=(CustomView)findViewById(R.id.customView);
        Button b=(Button)findViewById(R.id.Button01);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cv.updatePosition();

            }
        });
    }


    @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.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }


}




package com.provegrafica3;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import java.util.Random;

public class CustomView extends View {
    Bitmap bm;
    Random r;
    Paint p;

    int w,h,bw,bh;
    int px=-1,py=-1;
    public CustomView(Context c){
        super(c);
        bm= BitmapFactory.decodeResource(c.getResources(), R.drawable.imm);
        bw=bm.getWidth(); //larghezza bitmap
        bh=bm.getHeight();//altezza
        p=new Paint(); // pennello
        p.setColor(Color.CYAN);
        p.setAntiAlias(true);
        r=new Random();
    }
    public CustomView(Context c,AttributeSet att){
        super(c,att);
        bm= BitmapFactory.decodeResource(c.getResources(), R.drawable.imm);
        bw=bm.getWidth(); //larghezza bitmap
        bh=bm.getHeight();//altezza
        p=new Paint(); // pennello
        p.setColor(Color.CYAN);
        p.setAntiAlias(true);
        r=new Random();
    }
    public CustomView(Context c,AttributeSet att,int s){
        super(c,att,s);
        bm= BitmapFactory.decodeResource(c.getResources(), R.drawable.imm);
        bw=bm.getWidth(); //larghezza bitmap
        bh=bm.getHeight();//altezza
        p=new Paint(); // pennello
        p.setColor(Color.CYAN);
        p.setAntiAlias(true);
        r=new Random();
    }
    @Override
    protected void onDraw(Canvas c) {
        super.onDraw(c);
        if(px==-1&&py==-1){ // se non abbiamo ancora settato le coordinate, posizioniamo la bmp al centro
            px=w/2-bw/2; //metà della larghezza view, meno metà della figura
            py=h/2-bh/2; //metà dell'altezza view, meno metà della figura
        }


        c.drawCircle(px+(bw/2), py+(bh/2), 70, p); //disegnamo un cerchio con centro al centro della bitmap
        c.drawBitmap(bm,px,py,null);


    }

    public void updatePosition(){
        //posizione random x,y della bitmap all interno della view
        px=r.nextInt(w-bw);
        py=r.nextInt(h-bh);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //registriamo le dimensioni della view
        w=MeasureSpec.getSize(widthMeasureSpec);
        h=MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(w,h);
    }
}


la parte del file xml

<com.provegrafica3.MainActivity.CustomView
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       android:id="@+id/customView"/>




java.lang.NoSuchMethodException: com.provegrafica3.MainActivity$CustomView.<init>(android.content.Context, android.util.AttributeSet)
   at java.lang.Class.getConstructor0(Class.java:2810)
   at java.lang.Class.getConstructor(Class.java:1718)
   at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:300)
   at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:97)
   at com.android.tools.idea.rendering.ProjectCallback.loadView(ProjectCallback.java:148)
   at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
   at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
   at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:755)
   at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:373)
   at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:400)
   at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:336)
   at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:332)
   at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:519)
   at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:512)
   at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:939)
   at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:512)
   at com.android.tools.idea.rendering.RenderService.render(RenderService.java:549)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.compute(AndroidLayoutPreviewToolWindowManager.java:571)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.compute(AndroidLayoutPreviewToolWindowManager.java:564)
   at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:950)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:564)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:83)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6$1.run(AndroidLayoutPreviewToolWindowManager.java:518)
   at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
   at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
   at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
   at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6.run(AndroidLayoutPreviewToolWindowManager.java:513)
   at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
   at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
   at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
   at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
   at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
   at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
   at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:237)
   at com.intellij.util.Alarm$Request$1.run(Alarm.java:297)
   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
   at java.util.concurrent.FutureTask.run(FutureTask.java:262)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
   at java.lang.Thread.run(Thread.java:744)






Offline Paso

  • Utente junior
  • **
  • Post: 98
  • Respect: +17
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy sIII mini
  • Sistema operativo:
    Linux Mint 17 64-bit
Re:java.lang.NoSuchMethodException con View personalizzata
« Risposta #1 il: 25 Ottobre 2013, 10:31:00 CEST »
0
Comincia a leggerti questo: http://wiki.simplemachines.org/smf/Code se no la vedo dura aiutarti!

Offline BraganKimaru

  • Nuovo arrivato
  • *
  • Post: 3
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    samsung galaxy s4
  • Sistema operativo:
    windows 7
Re:java.lang.NoSuchMethodException con View personalizzata
« Risposta #2 il: 25 Ottobre 2013, 10:46:33 CEST »
0
MainActivity

Codice: [Seleziona]

package com.provegrafica3;


import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.logging.Handler;

public class MainActivity extends ActionBarActivity {
    CustomView cv;
    Handler updateCvHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        cv=(CustomView)findViewById(R.id.customView);
        Button b=(Button)findViewById(R.id.Button01);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cv.updatePosition();

            }
        });
    }


    @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.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }


}



La View modificata

Codice: [Seleziona]

package com.provegrafica3;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import java.util.Random;

public class CustomView extends View {
    Bitmap bm;
    Random r;
    Paint p;

    int w,h,bw,bh;
    int px=-1,py=-1;
    public CustomView(Context c){
        super(c);
        bm= BitmapFactory.decodeResource(c.getResources(), R.drawable.imm);
        bw=bm.getWidth(); //larghezza bitmap
        bh=bm.getHeight();//altezza
        p=new Paint(); // pennello
        p.setColor(Color.CYAN);
        p.setAntiAlias(true);
        r=new Random();
    }
    public CustomView(Context c,AttributeSet att){
        super(c,att);
        bm= BitmapFactory.decodeResource(c.getResources(), R.drawable.imm);
        bw=bm.getWidth(); //larghezza bitmap
        bh=bm.getHeight();//altezza
        p=new Paint(); // pennello
        p.setColor(Color.CYAN);
        p.setAntiAlias(true);
        r=new Random();
    }
    public CustomView(Context c,AttributeSet att,int s){
        super(c,att,s);
        bm= BitmapFactory.decodeResource(c.getResources(), R.drawable.imm);
        bw=bm.getWidth(); //larghezza bitmap
        bh=bm.getHeight();//altezza
        p=new Paint(); // pennello
        p.setColor(Color.CYAN);
        p.setAntiAlias(true);
        r=new Random();
    }
    @Override
    protected void onDraw(Canvas c) {
        super.onDraw(c);
        if(px==-1&&py==-1){ // se non abbiamo ancora settato le coordinate, posizioniamo la bmp al centro
            px=w/2-bw/2; //metà della larghezza view, meno metà della figura
            py=h/2-bh/2; //metà dell'altezza view, meno metà della figura
        }


        c.drawCircle(px+(bw/2), py+(bh/2), 70, p); //disegnamo un cerchio con centro al centro della bitmap
        c.drawBitmap(bm,px,py,null);


    }

    public void updatePosition(){
        //posizione random x,y della bitmap all interno della view
        px=r.nextInt(w-bw);
        py=r.nextInt(h-bh);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //registriamo le dimensioni della view
        w=MeasureSpec.getSize(widthMeasureSpec);
        h=MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(w,h);
    }
}



la parte del file xml

Codice: [Seleziona]

<com.provegrafica3.MainActivity.CustomView
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       android:id="@+id/customView"/>


L'errore che mi da

Codice: [Seleziona]

java.lang.NoSuchMethodException: com.provegrafica3.MainActivity$CustomView.<init>(android.content.Context, android.util.AttributeSet)
   at java.lang.Class.getConstructor0(Class.java:2810)
   at java.lang.Class.getConstructor(Class.java:1718)
   at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:300)
   at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:97)
   at com.android.tools.idea.rendering.ProjectCallback.loadView(ProjectCallback.java:148)
   at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
   at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
   at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:755)
   at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:373)
   at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:400)
   at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:336)
   at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:332)
   at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:519)
   at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:512)
   at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:939)
   at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:512)
   at com.android.tools.idea.rendering.RenderService.render(RenderService.java:549)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.compute(AndroidLayoutPreviewToolWindowManager.java:571)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.compute(AndroidLayoutPreviewToolWindowManager.java:564)
   at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:950)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:564)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:83)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6$1.run(AndroidLayoutPreviewToolWindowManager.java:518)
   at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
   at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
   at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
   at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
   at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6.run(AndroidLayoutPreviewToolWindowManager.java:513)
   at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
   at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
   at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
   at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
   at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
   at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
   at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:237)
   at com.intellij.util.Alarm$Request$1.run(Alarm.java:297)
   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
   at java.util.concurrent.FutureTask.run(FutureTask.java:262)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
   at java.lang.Thread.run(Thread.java:744)