2017-05-10 11 views
-1

私は初心者アンドロイド開発者です。 イメージスライダーを表示するアンドロイドアプリケーションを作成しようとしていました。 「アダプタ」を使用し、アダプタオブジェクト「アダプタ」をインスタンス化しようとしました。 しかし、エラーメッセージが表示され、「エラー:(18、18)エラー:アダプターが抽象です。インスタンス化することはできません。 ' 私はこの問題の解決策を探していましたが、Stackoverflowの誰かがAdapterをインスタンス化すると言っていました。まず、AdapterクラスがPageAdapterクラスを継承する必要があります。私は次のようにしましたが、同じエラーがまだ出ています。私のコードで何が間違っていますか? 以下はAdapterクラスのコードである 'Adapter.java'です。androidError:(18、18)エラー:アダプタは抽象です。インスタンス化できません

package com.example.clee.tutorial9; 

import android.content.Context; 
import android.support.v4.view.PagerAdapter; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 


public class Adapter extends PagerAdapter { 

private int[] images= {R.drawable.one, R.drawable.two, R.drawable.three}; 
//one, two, three are the name of the images. 

private LayoutInflater inflater; 
private Context context; 

public Adapter(Context context){ 
    this.context= context; 
} 

@Override 
public int getCount() { 
    return images.length; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view==((LinearLayout) object); 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position){ 
    inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v= inflater.inflate(R.layout.slider, container, false); 
    ImageView imageview= (ImageView) v.findViewById(R.id.imageView); 
    TextView textview= (TextView) v.findViewById(R.id.textView); 
    imageview.setImageResource(images[position]); 
    textview.setText((position +1) + "image."); 
    container.addView(v); 
    return v; 
    } 

@Override 
public void destroyItem(ViewGroup container, int position, Object object){ 
    container.invalidate(); 
    } 
} 

これは 'MainActivity.java'のコードです。

package com.example.clee.tutorial9; 

import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Adapter; 


public class MainActivity extends AppCompatActivity { 

Adapter adapter; 
ViewPager viewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    viewPager= (ViewPager) findViewById(R.id.view); 
    adapter= new Adapter(this); 
    viewPager.setAdapter(adapter); 
    } 
} 

「MainActivity.java」の19番目と20番目の文に誤りがあります。 要約すると、コードに2つのエラーがあります。

  1. アダプターは抽象的なです:インスタンス化することはできません。

  2. 互換性のないタイプ:アダプターをPageAdapterに変換できません。

+0

あなたの質問に、より良い答えが見つかりましたか? –

答えて

1

アダプタクラスの名前をCustomAdapterまたは他の名前に変更します。 あなたが現在android.widget.Adapter

0

を参照しているので、あなたが間違ったパッケージをインポートしているので、あなたは間違ったアダプタクラスを使用している:android.widget.Adapter;

インポートを代わりにこの:

com.example.clee.tutorial9.Adapter 

より良い、あなたを定義いけませんSDKに既に定義されているクラスがあるので、名前を変更しますアダプタMyAppAdampterとインポート

com.example.clee.tutorial9.MyAppAdampter