我正在尝试准备动态导航抽屉。我正在使用RecylerView。我的代码在这里:
Listele.java
package com.example.hp.myapplication;
/**
* Created by hp on 25.7.2016.
*/
public class Listele {
private String adi;
private String resim;
public Listele(String adi, String resim) {
this.adi = adi;
this.resim = resim;
}
public String getAdi() {
return adi;
}
public void setAdi(String adi) {
this.adi = adi;
}
public String getResim() {
return resim;
}
public void setResim(String resim) {
this.resim = resim;
}
@Override
public String toString() {
return "Listele{" +
"adi='" + adi + '\'' +
", resim='" + resim + '\'' +
'}';
}
}
ListeleAdapter.java
package com.example.hp.myapplication;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
/**
* Created by hp on 25.7.2016.
*/
public class ListeleAdapter extends RecyclerView.Adapter<ListeleAdapter.urunlistem> {
private Context contexto;
private ArrayList<Listele>tortica;
public ListeleAdapter(Context contexto, ArrayList<Listele> tortica) {
this.contexto = contexto;
this.tortica = tortica;
}
@Override
public urunlistem onCreateViewHolder(ViewGroup parent, int viewType) {
return new urunlistem(LayoutInflater.from(parent.getContext()) .inflate(R.layout.list,null));
}
@Override
public void onBindViewHolder(urunlistem holder, int position) {
holder.adi.setText(tortica.get(position).getAdi());
Glide.with(contexto).load("http://192.168.1.33:8080/urunler/"+tortica.get(position).getResim()).into(holder.tresim);
}
@Override
public int getItemCount() {
return tortica.size();
}
public class urunlistem extends RecyclerView.ViewHolder{
ImageView tresim;
TextView adi;
public urunlistem(View itemView) {
super(itemView);
tresim=(ImageView)itemView.findViewById(R.id.resim);
adi=(TextView)itemView.findViewById(R.id.adi);
}
}
}
MainActivity.java
package com.example.hp.myapplication;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final String url="http://192.168.1.33:8080/urunler/goruntule2.php";
private RecyclerView recyclerView;
private ListeleAdapter adapter;
private ArrayList<Listele> listeles = new ArrayList<>();
ProgressDialog progressDialog;
RequestQueue requestQu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
requestQu = Volley.newRequestQueue(getApplicationContext());
goster();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void goster() {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Bilgiler alınıyor");
recyclerView=(RecyclerView) findViewById(R.id.listado);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,false));
recyclerView.setHasFixedSize(true);
adapter = new ListeleAdapter(this, listeles);
recyclerView.setAdapter(adapter);
JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i=0;i<response.length();i++)
{
try {
JSONObject object = response.getJSONObject(i);
String resimurl=object.getString("resim");
String adi=object.getString("adi");
listeles.add(new Listele(adi,resimurl));
} catch (JSONException e) {
Toast.makeText(MainActivity.this,""+e,Toast.LENGTH_LONG).show();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQu.add(request);
}
}
修改 它给出了错误,如
E / AndroidRuntime:致命异常:主要流程: com.example.hp.myapplication,PID:7960 java.lang.RuntimeException: 无法开始活动 ComponentInfo {com.example.hp.myapplication / com.example.hp.myapplication.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 在android.app.ActivityThread.access $ 800(ActivityThread.java:135)at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 在android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:136)at android.app.ActivityThread.main(ActivityThread.java:5001)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)at dalvik.system.NativeStart.main(Native方法)引起: java.lang.NullPointerException at com.example.hp.myapplication.MainActivity.goster(MainActivity.java:123) 在 com.example.hp.myapplication.MainActivity.onCreate(MainActivity.java:49) 在android.app.Activity.performCreate(Activity.java:5231)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 在android.app.ActivityThread.access $ 800(ActivityThread.java:135)at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 在android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:136)at android.app.ActivityThread.main(ActivityThread.java:5001)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)at dalvik.system.NativeStart.main(Native Method)D / dalvikvm: GC_FOR_ALLOC释放378K,13%免费3267K / 3716K,暂停8ms,总计8ms
[ 08-14 17:13:15.298 77: 77 D/ ] Socket deconnection [ 08-14 17:13:17.294 77: 77 D/ ] Socket deconnection [ 08-14 17:13:19.302 77: 77 D/ ] Socket deconnection [ 08-14 17:13:21.298 77: 77 D/ ] Socket deconnection [ 08-14 17:13:23.298 77: 77 D/ ] Socket deconnection [ 08-14 17:13:25.298 77: 77 D/ ] Socket deconnection [ 08-14 17:13:27.298 77: 77 D/ ] Socket deconnection [ 08-14 17:13:29.302 77: 77 D/ ] Socket deconnection [ 08-14 17:13:31.310 77: 77 D/ ] Socket deconnection Disconnected from the target VM, address: 'localhost:8615', transport: 'socket'