2016-05-27 7 views
0

ビューページャアダプタが動作しません。以下のコードを.javaと.xmlのような別のファイルに投稿しました。 問題を親切に解決してください。ヘルパーに感謝します。ので、ここでAndroid:アクティビティがViewPagerアダプタのためにクラッシュする

package com.example.abbas.exmpression; 
    import android.app.Activity; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 

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

    public class view_pager2 extends PagerAdapter { 

    private int[] image_resources= 
    {R.drawable.centre,R.drawable.left, 
    R.drawable.right,R.drawable.face_guide_center}; 
    private Context ctx; 
    private LayoutInflater layoutInflater; 
    public view_pager2(Context ctx){ 
     this.ctx= ctx; 
    } 
    @Override 
    public int getCount() { 
     return image_resources.length; 
    } 
    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return (view ==(LinearLayout)object); 
    } 
    @Override 
    public Object instantiateItem(ViewGroup container, int position){ 
    layoutInflater= (LayoutInflater) 
    ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View item_view = 
    layoutInflater.inflate(R.layout.activity_third_activity 
    ,container,false); 
    ImageView imageView = (ImageView) 
    item_view.findViewById(R.id.cam_images); 
    TextView textView= (TextView) 
    item_view.findViewById(R.id.act3_txt2); 
    imageView.setImageResource(image_resources[position]); 
    if(position==0) 
    { 
     textView.setText("abc"); 
    } 
    else if(position==1) 
    { 
     textView.setText("bla bla "); 
    } 
    else if(position==2) 
    { 
     textView.setText("ha ha ha "); 
    } 
    else 
    { 
     textView.setText("akjsdkasjdkl"); 
    } 
    container.addView(item_view); 
    return item_view; 
} 
@Override 
public void destroyItem(ViewGroup container, int position, Object 
object) { 

    container.removeView((LinearLayout)object); 
    } 
} 

third_activity.xml

<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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    > 
    <android.support.v4.view.ViewPager 
     android:id="@+id/screen3" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     > 
    </android.support.v4.view.ViewPager> 
    <TextView 
     android:id="@+id/act3_txt1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Show us your face, &amp; some moves!" 
     android:textSize="35sp" 
     android:textAlignment="center" 
     android:paddingBottom="20dp" 
     /> 
    <ImageView 
     android:id="@+id/cam_images" 
     android:layout_height="250sp" 
     android:layout_width="wrap_content" 
     android:src="@drawable/centre" 
     android:layout_below="@+id/act3_txt1" 
     android:paddingBottom="10dp" 
     /> 
    <TextView 
     android:id="@+id/act3_txt2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Align your face &amp; hold to start Recording" 
     android:layout_below="@+id/cam_images" 
     android:textSize="35sp" 
     android:textAlignment="center" 
     /> 
</RelativeLayout> 

third_activity.java アダプタ=新しいview_pager2(この)私はそれが何であるかを得ていないmは、問題が発生しています。助けが必要です。

public class third_activity extends Activity 
{ 
    ViewPager viewPager; 
    view_pager2 adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(getWindow().FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.activity_third_activity); 
     viewPager= (ViewPager)findViewById(R.id.screen3); 
     adapter= new view_pager2(this); 
     viewPager.setAdapter(adapter); 
    } 
} 
+4

ポスト 'Logcat'、これをしてください試してみてください。 –

+0

問題が解決しました.2つのバグが見つかりました。 1つ:ピクチャディメンションがより高くなった。 2:私はxmlファイルでRelativeLayoutを使用していましたが、javaコードでは、メソッド "isViewFromObject"にLinearLayoutを書きました。 –

答えて

0

あなたの活動に

adapter= new view_pager2(third_activity.this); 
あなたのアダプタクラスで

Activity ctx; 
    public view_pager2(Activity ctx){ 
     this.ctx= ctx; 
    } 
+0

それでも、残念ながら*アプリケーション*は停止しています! –

+0

@ p116045AbbasRazaあなたのlogcatを投稿してください – Nisarg

+0

AndroidとStackOverFlowにもnoobがあります。エラーが発生したときにlogcatがどこに行くのかわからないので、突然多くのスクロールが突然消えてしまいます。どうすればいい? –

関連する問題