Hello Every One我试图在android studio中实现Recycler视图,但我遇到了崩溃错误,当我触摸回收器视图的第一项时,我希望它移动到其他活动,但应用程序崩溃了这样做,我做错了什么?
这是我的代码Activity.java
package com.recycler.hp.navigationbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class PakistaniActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pakistani);
RecyclerView rl=findViewById(R.id.list);
rl.setLayoutManager(new LinearLayoutManager(this));
String[] languges={"Badnamiyan", "song2","song3","song4","Song5","song6","song7","song8","song9"};
rl.setAdapter(new Adaptor(languges,getApplicationContext()));
}
}
Adator.java(适配器类)
package com.recycler.hp.navigationbar;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.net.URL;
import static java.security.AccessController.getContext;
public class Adaptor extends
RecyclerView.Adapter<Adaptor.viewholder> {
private Context context;
private String[] data;
public Adaptor(String[] data, Context context) {
this.data = data;
this.context = context;
}
@Override
public viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_item1, parent, false);
return new viewholder(view);
}
@Override
public void onBindViewHolder(viewholder holder, final int position) {
String title = data[position];
holder.t.setText(title);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(position==0)
{
Intent intent=new Intent(context,Revival_Activity.class);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return data.length;
}
public class viewholder extends RecyclerView.ViewHolder {
ImageView i;
TextView t;
public viewholder(View itemView) {
super(itemView);
i = itemView.findViewById(R.id.img1);
t = itemView.findViewById(R.id.main1);
}
}
}
logcat的
04-15 13:41:14.140 885-885/? E/Trace: error opening trace file: No such file or directory (2)
04-15 13:41:14.210 885-885/com.recycler.hp.navigationbar W/dalvikvm: Refusing to reopen boot DEX '/system/framework/hwframework.jar'
04-15 13:41:14.920 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve interface method 20421: Landroid/view/Window$Callback;.onPointerCaptureChanged (Z)V
04-15 13:41:14.920 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve interface method 20423: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
04-15 13:41:14.930 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
04-15 13:41:14.930 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve interface method 20425: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
04-15 13:41:14.930 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve interface method 20429: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
04-15 13:41:14.930 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 732: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-15 13:41:14.950 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 754: Landroid/content/res/TypedArray;.getType (I)I
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 512: Landroid/content/Context;.createDeviceProtectedStorageContext ()Landroid/content/Context;
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 518: Landroid/content/Context;.getCodeCacheDir ()Ljava/io/File;
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 519: Landroid/content/Context;.getColor (I)I
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 520: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 522: Landroid/content/Context;.getDataDir ()Ljava/io/File;
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 523: Landroid/content/Context;.getDrawable (I)Landroid/graphics/drawable/Drawable;
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 525: Landroid/content/Context;.getExternalCacheDirs ()[Ljava/io/File;
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 527: Landroid/content/Context;.getExternalFilesDirs (Ljava/lang/String;)[Ljava/io/File;
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 530: Landroid/content/Context;.getNoBackupFilesDir ()Ljava/io/File;
04-15 13:41:15.060 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 532: Landroid/content/Context;.getObbDirs ()[Ljava/io/File;
04-15 13:41:15.070 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 543: Landroid/content/Context;.isDeviceProtectedStorage ()Z
04-15 13:41:15.070 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 558: Landroid/content/Context;.startForegroundService (Landroid/content/Intent;)Landroid/content/ComponentName;
04-15 13:41:15.440 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 14452: Landroid/support/v7/view/ContextThemeWrapper;.createConfigurationContext (Landroid/content/res/Configuration;)Landroid/content/Context;
04-15 13:41:15.620 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 20936: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
04-15 13:41:15.780 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 20304: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
04-15 13:41:15.800 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 520: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
04-15 13:41:15.850 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/graphics/drawable/Icon;)
04-15 13:41:15.850 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 20961: Landroid/widget/ImageButton;.setImageIcon (Landroid/graphics/drawable/Icon;)V
04-15 13:41:15.850 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 695: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-15 13:41:15.850 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 697: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-15 13:41:15.920 885-885/com.recycler.hp.navigationbar E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
04-15 13:41:15.920 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve instanceof 199 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
04-15 13:41:16.080 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21293: Landroid/widget/TextView;.getAutoSizeMaxTextSize ()I
04-15 13:41:16.080 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21294: Landroid/widget/TextView;.getAutoSizeMinTextSize ()I
04-15 13:41:16.080 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21295: Landroid/widget/TextView;.getAutoSizeStepGranularity ()I
04-15 13:41:16.090 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21296: Landroid/widget/TextView;.getAutoSizeTextAvailableSizes ()[I
04-15 13:41:16.090 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21297: Landroid/widget/TextView;.getAutoSizeTextType ()I
04-15 13:41:16.090 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21347: Landroid/widget/TextView;.setAutoSizeTextTypeUniformWithConfiguration (IIII)V
04-15 13:41:16.090 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21348: Landroid/widget/TextView;.setAutoSizeTextTypeUniformWithPresetSizes ([II)V
04-15 13:41:16.090 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21349: Landroid/widget/TextView;.setAutoSizeTextTypeWithDefaults (I)V
04-15 13:41:16.290 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21295: Landroid/widget/TextView;.getAutoSizeStepGranularity ()I
04-15 13:41:16.310 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve static method 19624: Landroid/text/StaticLayout$Builder;.obtain (Ljava/lang/CharSequence;IILandroid/text/TextPaint;I)Landroid/text/StaticLayout$Builder;
04-15 13:41:16.310 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 21338: Landroid/widget/TextView;.isInLayout ()Z
04-15 13:41:16.370 885-885/com.recycler.hp.navigationbar W/dalvikvm: Link of class 'Landroid/support/v4/widget/DrawerLayout$1;' failed
04-15 13:41:16.370 885-885/com.recycler.hp.navigationbar E/dalvikvm: Could not find class 'android.support.v4.widget.DrawerLayout$1', referenced from method android.support.v4.widget.DrawerLayout.<init>
04-15 13:41:16.370 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve new-instance 1676 (Landroid/support/v4/widget/DrawerLayout$1;) in Landroid/support/v4/widget/DrawerLayout;
04-15 13:41:16.410 885-885/com.recycler.hp.navigationbar E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onDraw
04-15 13:41:16.410 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve check-cast 2441 (Landroid/view/WindowInsets;) in Landroid/support/v4/widget/DrawerLayout;
04-15 13:41:16.420 885-885/com.recycler.hp.navigationbar E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onMeasure
04-15 13:41:16.420 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve check-cast 2441 (Landroid/view/WindowInsets;) in Landroid/support/v4/widget/DrawerLayout;
04-15 13:41:16.420 885-885/com.recycler.hp.navigationbar E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onMeasure
04-15 13:41:16.420 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve check-cast 2441 (Landroid/view/WindowInsets;) in Landroid/support/v4/widget/DrawerLayout;
04-15 13:41:16.430 885-885/com.recycler.hp.navigationbar W/dalvikvm: Link of class 'Landroid/support/v4/widget/DrawerLayout$1;' failed
04-15 13:41:16.600 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/graphics/drawable/Icon;)
04-15 13:41:16.600 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 20999: Landroid/widget/ImageView;.setImageIcon (Landroid/graphics/drawable/Icon;)V
04-15 13:41:16.970 885-885/com.recycler.hp.navigationbar W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
04-15 13:41:17.250 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 20229: Landroid/view/ViewGroup$MarginLayoutParams;.getLayoutDirection ()I
04-15 13:41:17.250 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 20233: Landroid/view/ViewGroup$MarginLayoutParams;.resolveLayoutDirection (I)V
04-15 13:41:17.250 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 20234: Landroid/view/ViewGroup$MarginLayoutParams;.setLayoutDirection (I)V
04-15 13:41:17.250 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 20235: Landroid/view/ViewGroup$MarginLayoutParams;.setMarginEnd (I)V
04-15 13:41:17.250 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 20236: Landroid/view/ViewGroup$MarginLayoutParams;.setMarginStart (I)V
04-15 13:41:17.390 885-885/com.recycler.hp.navigationbar I/Adreno200-EGL: <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.01.21.010_msm8625_JB_REL_2.0.3_Merge_release_AU (Merge)
Build Date: 10/26/12 Fri
Local Branch:
Remote Branch: quic/jb_rel_2.0.3
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.01.21.010 + NOTHING
04-15 13:41:17.790 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve static method 1553: Landroid/os/Trace;.beginSection (Ljava/lang/String;)V
04-15 13:41:17.790 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve static method 1554: Landroid/os/Trace;.endSection ()V
04-15 13:41:17.830 885-885/com.recycler.hp.navigationbar W/dalvikvm: VFY: unable to resolve virtual method 16883: Landroid/support/v7/widget/LinearLayoutCompat;.drawableHotspotChanged (FF)V
04-15 13:41:18.190 885-885/com.recycler.hp.navigationbar I/Choreographer: Skipped 55 frames! The application may be doing too much work on its main thread.
04-15 13:41:22.930 885-885/com.recycler.hp.navigationbar W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40e6f438)
答案 0 :(得分:0)
您需要替换
rl.setAdapter(new Adaptor(languges,getApplicationContext()));
带
rl.setAdapter(new Adaptor(languges,this));
或
rl.setAdapter(new Adaptor(languges,PakistaniActivity.this));
您无法通过非活动的上下文显示应用程序窗口/对话框。