0
I have the following class below, where the side menu calls are, only what I want to do, if I implement all the menus in this class the project will get too big, and I wanted each menu to call a different class, as for example:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_markets) {
new DownloadJsonAsyncTask().execute("https://bittrex.com/api/v1.1/public/getmarkets");
}
I tried to implement something like ,but the program closes when I click on the menu there. I would like an example as I could do.
package br.com.escconsultoria.bittrex.view;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import br.com.escconsultoria.bittrex.R;
public class BitTrexActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
String data = "";
TableLayout tl;
TableRow tr;
TextView label;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bit_trex);
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);
//TODO
new DownloadJsonAsyncTask().execute("https://bittrex.com/api/v1.1/public/getmarkets");
}
@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.bit_trex, 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.
int id = item.getItemId();
if (id == R.id.nav_markets) {
new DownloadJsonAsyncTask().execute("https://bittrex.com/api/v1.1/public/getmarkets");
} /*else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}*/
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
/*@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Trend trend = (Trend) l.getAdapter().getItem(position);
Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(trend.url));
startActivity(it);
}*/
class DownloadJsonAsyncTask extends AsyncTask<String, Void, List<Trend>> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(BitTrexActivity.this, "Aguarde","Baixando JSON, Por Favor Aguarde...");
}
@Override
protected List<Trend> doInBackground(String... params) {
String urlString = params[0];
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(urlString);
try {
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String json = toString(instream);
instream.close();
List<Trend> trends = getTrends(json);
return trends;
}
} catch (Exception e) {
Log.e("DEVMEDIA", "Falha ao acessar Web service", e);
}
return null;
}
private List<Trend> getTrends(String jsonString) {
List<Trend> trends = new ArrayList<Trend>();
try {
JSONObject trendLists = new JSONObject(jsonString);
//JSONObject trendList = trendLists.getgetJSONObject(0);
JSONArray trendsArray = trendLists.getJSONArray("result");
JSONObject trend;
for (int i = 0; i < trendsArray.length(); i++) {
trend = new JSONObject(trendsArray.getString(i));
Log.i("DEVMEDIA", "nome=" + trend.getString("MarketCurrency"));
Trend objetoTrend = new Trend();
objetoTrend.name = trend.getString("MarketCurrency");
objetoTrend.url = trend.getString("BaseCurrency");
trends.add(objetoTrend);
}
} catch (JSONException e) {
Log.e("DEVMEDIA", "Erro no parsing do JSON", e);
}
return trends;
}
@Override
protected void onPostExecute(List<Trend> result) {
super.onPostExecute(result);
dialog.dismiss();
Log.e("DEVMEDIA", "Passou No onPostExecute");
//parse json data
try {
//JSONArray jArray = new JSONArray(result);
TableLayout tv = (TableLayout) findViewById(R.id.tableLayout);
tv.removeAllViewsInLayout();
int flag = 1;
Log.e("DEVMEDIA", result.toString());
for (int i = -1; i < result.size() - 1; i++) {
TableRow tr = new TableRow(BitTrexActivity.this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView b6 = new TextView(BitTrexActivity.this);
b6.setText("Id");
b6.setTextColor(Color.BLUE);
b6.setTextSize(15);
tr.addView(b6);
TextView b19 = new TextView(BitTrexActivity.this);
b19.setPadding(10, 0, 0, 0);
b19.setTextSize(15);
b19.setText("Name");
b19.setTextColor(Color.BLUE);
tr.addView(b19);
TextView b29 = new TextView(BitTrexActivity.this);
b29.setPadding(10, 0, 0, 0);
b29.setText("Status");
b29.setTextColor(Color.BLUE);
b29.setTextSize(15);
tr.addView(b29);
tv.addView(tr);
final View vline = new View(BitTrexActivity.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
Log.e("DEVMEDIA", "flag: " + flag);
} else {
Trend trend = (Trend) result.get(i);
TextView b = new TextView(BitTrexActivity.this);
String stime = trend.name;
Log.e("DEVMEDIA", "Passou POR AQUI ");
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1 = new TextView(BitTrexActivity.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
String stime1 = trend.url;
b1.setText(stime1);
b1.setTextColor(Color.BLACK);
tr.addView(b1);
TextView b2 = new TextView(BitTrexActivity.this);
b2.setPadding(10, 0, 0, 0);
String stime2 = trend.name;
b2.setText(stime2);
b2.setTextColor(Color.BLACK);
b2.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
final View vline1 = new View(BitTrexActivity.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
}
} catch (Exception e) {
}
/*if (result.size() > 0) {
ArrayAdapter<Trend> adapter = new ArrayAdapter<Trend>(
BitTrexActivity.this,
android.R.layout.simple_list_item_1, result);
tableLayout.set.setAdapter(adapter);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
BitTrexActivity.this).setTitle("Atenção")
.setMessage("Não foi possivel acessar essas informções...")
.setPositiveButton("OK", null);
builder.create().show();
}*/
}
private String toString(InputStream is) throws IOException {
byte[] bytes = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int lidos;
while ((lidos = is.read(bytes)) > 0) {
baos.write(bytes, 0, lidos);
}
return new String(baos.toByteArray());
}
}*/
}
class Trend {
String name;
String url;
@Override
public String toString() {
return name;
}
}
Error:
FATAL EXCEPTION: main
Process: br.com.escconsultoria.bittrex, PID: 24623
android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.escconsultoria.bittrex/br.com.escconsultoria.bittrex.Download.DownloadGetMarkers}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1723)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1511)
at android.app.Activity.startActivityForResult(Activity.java:3473)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:3434)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:708)
at android.app.Activity.startActivity(Activity.java:3676)
at android.app.Activity.startActivity(Activity.java:3644)
at br.com.escconsultoria.bittrex.view.BitTrexActivity.onNavigationItemSelected(BitTrexActivity.java:89)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:156)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:821)
at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:88)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:968)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:342)
at android.view.View.performClick(View.java:4560)
at android.view.View$PerformClick.run(View.java:18636)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
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:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
What mistake are you having? Send the stack for us to try to help
– Ezequiel Messore
added the stack, saw the error that is not declared in Android Manifest, but is wrong this way you are trying to do I wanted it to reuse the content_bit_trex.xml that is already created.
– Ederson Coelho
and commenting on the error I tried to put in Android Manifest and nothing appears, is not the way I wanted, comes the blank screen with nothing.
– Ederson Coelho