的build.gradle
build.gradle文件库的所有依赖和插件,并在其中定义一些其他必需的配置。
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.admin.app_shewale"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:recyclerview-v7:24.0.0-beta1'
compile files('src/main/java/libs/YouTubeAndroidPlayerApi.jar')
compile 'com.android.support:cardview-v7:24.0.0-beta1'
compile files('src/main/java/libs/volley.jar')
}
Mainactivity.java
这是包含recycleview的mainclass,并且还通过使用volly库发送请求到服务器获取jsonarray得到jsonarray解析数据然后绑定到list,发送到adapter.this类也定义onscrollchangelistener每当userreach结束列表显示进度条从服务器加载更多数据后
package com.example.gallery;
import android.annotation.TargetApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.admin.app_shewale.R;
import com.example.vikaskame.VikasGallery;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Gallery_byVolly extends AppCompatActivity implements RecyclerView.OnScrollChangeListener {
RecyclerView gallerygridview1;
TextView gallerytext;
List<VikasGallery> gallerylist1;
private RequestQueue requestQueue;
CardAdapter adapter;
private int requestCount = 1;
public static final String DATA_URL= "http://1biz.in/shevale_app/android/shevale.php?page=";
private RecyclerView.LayoutManager layoutManager;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_by_volly);
gallerygridview1 = (RecyclerView) findViewById(R.id.vollygallerygridview);
gallerygridview1.setHasFixedSize(true);
layoutManager = new GridLayoutManager(this,2);
gallerygridview1.setLayoutManager(layoutManager);
gallerylist1 = new ArrayList<>();
requestQueue = Volley.newRequestQueue(this);
//Calling method to get data to fetch data
getData();
//Adding an scroll change listener to recyclerview
gallerygridview1.setOnScrollChangeListener(this);
System.out.println("value of list in parse m" + gallerylist1);
//initializing our adapter
adapter = new CardAdapter(gallerylist1, this);
//Adding adapter to recyclerview
gallerygridview1.setAdapter(adapter);
}
//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount)
{
//Initializing ProgressBar
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//Displaying Progressbar
progressBar.setVisibility(View.VISIBLE);
setProgressBarIndeterminateVisibility(true);
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCount),new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response)
{
//Calling method parseData to parse the json response
System.out.println("value of response is"+response);
parseData(response);
//Hiding the progressbar
progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
Toast.makeText(Gallery_byVolly.this, "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
//This method will get data from the web api
private void getData()
{
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}
//This method will parse json data
private void parseData(JSONArray array)
{
for (int i = 0; i < array.length(); i++)
{
//Creating the superhero object
VikasGallery superHero = new VikasGallery();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);
//Adding data to the superhero object
superHero.setImagesurl(json.getString(Config.TAG_IMAGE_URL));
}
catch (JSONException e)
{
e.printStackTrace();
}
//Adding the superhero object to the list
gallerylist1.add(superHero);
System.out.println("value of list inside parse m" + gallerylist1);
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}
private boolean isLastItemDisplaying(RecyclerView recyclerView)
{
if (recyclerView.getAdapter().getItemCount() != 0)
{
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
return true;
}
return false;
}
//Overriden method to detect scrolling
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
//Ifscrolled at last then
if (isLastItemDisplaying(gallerygridview1))
{
//Calling the method getdata again
getData();
}
}
}
这是用于显示recyleview中所有图像的适配器类
获取构造函数中的记录列表并逐个显示所有图像。
Adapter.java
package com.example.gallery;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.admin.app_shewale.R;
import com.example.vikaskame.VikasGallery;
import java.util.ArrayList;
import java.util.List;
public class Gallery_VollyAdapter extends RecyclerView.Adapter<Gallery_VollyAdapter.ViewHolder> {
//Imageloader to load image
private ImageLoader imageLoader;
private Context context;
List<VikasGallery> superHeroes;
//Constructor of this class
public Gallery_VollyAdapter(List<VikasGallery> superHeroes, Context context){
super();
//Getting all superheroes
this.superHeroes = superHeroes;
this.context = context;
System.out.println("values of list is1"+superHeroes);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.vollyitemlist, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
superHeroes=new ArrayList<>();
//Getting the particular item from the list
VikasGallery superHero = superHeroes.get(position);
System.out.println("values of list is2"+superHeroes);
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImagesurl(), ImageLoader.getImageListener(holder.imageView, R.drawable.five, android.R.drawable.ic_dialog_alert));
//Showing data on the views
holder.imageView.setImageUrl(superHero.getImagesurl(), imageLoader);
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder
{
//Views
public NetworkImageView imageView;
//Initializing Views
public ViewHolder(View itemView)
{
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.vollyimage);
}
}
}
logcat的
执行5.0后,在logcat中显示这些类型的错误,显示出某种错误,我知道为什么会发生这种错误
但高于5.1工作正常
FATAL EXCEPTION: main
Process: com.example.admin.app_shewale, PID: 7783
java.lang.NoClassDefFoundError: com.example.gallery.Gallery_byVolly
at com.example.admin.app_shewale.MainActivity.onClick(MainActivity.java:165)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Mainactivity.class
这是定义所有功能的主类,并且当用户点击该按钮时显示所有主页按钮显示下一个活动
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
TextView homefirst, homesecond, homethird, homefourth, homefifth, homesix, homeseven, homeeight, homenine, hometen, homeeleve, hometwelve;
Boolean exit = false;
ViewFlipper viewFlipper;
private Animation slide_in_left;
ImageView firstimg, secondimg2, thirdimg3;
TextView slideretext;
private Handler handler=new Handler();
private int delay = 5000; //milliseconds
private int page = 0;
ViewPager sliderviewpager;
Slider_Adapter adapter;
List<Slidercls> imagesliderlist;
Runnable runnable = new Runnable()
{
public void run()
{
if (adapter.getCount() == page)
{
page = 0;
} else {
page++;
}
sliderviewpager.setCurrentItem(page, true);
handler.postDelayed(this, delay);
}
};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sliderviewpager=(ViewPager)findViewById(R.id.sliderviewpager);
imagesliderlist=getListData();
adapter = new Slider_Adapter(MainActivity.this,imagesliderlist);
sliderviewpager.setAdapter(adapter);
// sliderviewpager.setCurrentItem(0);
homefirst = (TextView) findViewById(R.id.homefirst);
homesecond = (TextView) findViewById(R.id.homesecond);
homethird = (TextView) findViewById(R.id.homethree);
homefourth = (TextView) findViewById(R.id.homefourth);
homefifth = (TextView) findViewById(R.id.homefive);
homesix = (TextView) findViewById(R.id.homesix);
homeseven = (TextView) findViewById(R.id.homeseven);
homeeight = (TextView) findViewById(R.id.homeeight);
homenine = (TextView) findViewById(R.id.homenine);
hometen = (TextView) findViewById(R.id.hometen);
homeeleve = (TextView) findViewById(R.id.homeeleven);
hometwelve = (TextView) findViewById(R.id.hometwelve);
homefirst.setOnClickListener(this);
homesecond.setOnClickListener(this);
homethird.setOnClickListener(this);
homefourth.setOnClickListener(this);
homefifth.setOnClickListener(this);
homesix.setOnClickListener(this);
homeseven.setOnClickListener(this);
homeeight.setOnClickListener(this);
homenine.setOnClickListener(this);
hometen.setOnClickListener(this);
homeeleve.setOnClickListener(this);
hometwelve.setOnClickListener(this);
}
private ArrayList<Slidercls> getListData()
{
ArrayList<Slidercls> listMockData = new ArrayList<Slidercls>();
String[] images = getResources().getStringArray(R.array.sliderimages);
String[] text=getResources().getStringArray(R.array.slidertext);
for (int i = 0; i < images.length; i++)
{
Slidercls newsData = new Slidercls();
newsData.setImageurl(images[i]);
newsData.setImgtitle(text[i]);
listMockData.add(newsData);
}
return listMockData;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.homefirst:
startActivity(new Intent(MainActivity.this, Viyaktigai_Mahitiactivity.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.homesecond:
startActivity(new Intent(MainActivity.this, Vikaskame.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.homethree:
startActivity(new Intent(MainActivity.this, Andolane_Activity.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.homefourth:
startActivity(new Intent(MainActivity.this, Katran_Activity.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.homefive:
startActivity(new Intent(MainActivity.this, Samajik_karyaActivity.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.homesix:
startActivity(new Intent(MainActivity.this, Sarvajanikutsav_Activity.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.homeseven:
startActivity(new Intent(MainActivity.this, com.example.gallery.Gallery_byVolly.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.homeeight:
startActivity(new Intent(MainActivity.this, VideoMain.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.homenine:
startActivity(new Intent(MainActivity.this, Mahtwcha_link_Activity.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.hometen:
startActivity(new Intent(MainActivity.this, Puraskar_Activity.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.homeeleven:
startActivity(new Intent(MainActivity.this, Profile_Activity.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
case R.id.hometwelve:
startActivity(new Intent(MainActivity.this, Sampark_Activity.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;
}
}
答案 0 :(得分:0)
请删除
compile files('src/main/java/libs/volley.jar')
并替换
compile 'com.android.volley:volley:1.0.0'
更新代码
startActivity(new Intent(MainActivity.this, Gallery_byVolly.class));
overridePendingTransition(R.anim.slideup, R.anim.nochange);
break;