[Q] Some problem with my code - Android Q&A, Help & Troubleshooting

Hi, I having some problem with this simple code. I don't undestrand because ADT impost fields as EditText, TextView and Button as field final. I think that there' some errore in my code, I admit that is one of my first times that i programming for andoird, so have patience with me.
I apologize if I wrong section :silly:
That's my code:
package com.example.buttoncerca;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class RisponditoreInterattivoActivity extends Activity implements Lista {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_risponditore_interattivo);
final EditText text = (EditText)this.findViewById(R.id.campoNome);
Button button = (Button)this.findViewById(R.id.buttone);
final TextView tv = (TextView)this.findViewById(R.id.testoSaluto);
button.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Editable nome = text.getText();
String s = nome.toString();
for(int c =0; c<3; c++){
if(toppings[c].equals(s)){
tv.setText(toppings[c] + " si butta nel " + cestino[c]);
} //End if
}//End for
}//End onClick
});
}
}
Click to expand...
Click to collapse
I implements inteface Lista, this is code:
package com.example.buttoncerca;
public interface Lista {
String[] toppings = {"Plastica", "Legno", "Cartone"};
String[] cestino = {"Cestino plastica", "Cestino legno", "Cestino cartone"};
}
Click to expand...
Click to collapse

And... what's the problem? Please post it "as is".

You need to instantiate the editable first, with BUFFERTYPE.EDITABLE, because in your current case you are casting a charseq to an editable so that's likely the exception you are getting (hard to know without a logcat). See here:
http://developer.android.com/reference/android/widget/EditText.html#getText()
Sent from my Amaze 4G using xda app-developers app

Related

[Q] [Dev] App Crashes instantly [problem solved]

Whats Wron with this? If I start the app it crashes instantly
It's only a Test app the name only is about what I plan to do in the future^^
Code:
package de.cwsurf.sorry.improvedBattery;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ImprovedBatteryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText eT = (EditText) findViewById(R.id.editText1);
eT.setText("Test");
eT.setText(eT.getText()+"\nTest1\nTest2\nTest3\nTest4");
setContentView(eT);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
EditText eT = (EditText) findViewById(R.id.editText1);
eT.setText(eT.getText()+"\nTest1\nTest2\nTest3\nTest4");
setContentView(eT);
}
});
}
}
problem solved. i only need to leave this away: setContentView(eT);

[Q] Linking Strings between Activities and WebViews

Hello XDA,
So I've not found anyone properly discussing this. If they do, please link me to the page and don't flame
I've been making an app for my school and so far, I've been doing alright. But now I have run into a problem which seems to be very hard to solve for me alone.
Namely, I have an EditText and a Button in one Activity and a WebView as a different Activity.
I'm trying to get the EditText String into the WebView URL (String being variable, Button initiating the WebView), but get a lot of errors, some not even related to that.
I would really appreciate it if someone could have a look at the code and help me with this
MyWebView.java:
Code:
package bas.sie.Antonius;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebView extends Activity {
WebView mWebView;
EditText mEtxtStudentNum;
static String StudentNumFromHome = bas.sie.Antonius.Home.StudentNum;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://carmelcollegegouda.nl/site_ant/roosters/standaardroosters/Lee1_" + StudentNumFromHome + ".htm");
}
}
Home.java (Homescreen):
Code:
package bas.sie.Antonius;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Home extends Activity {
Button mBtnStSchedule;
static EditText mEtxtStudentNum;
static final String StudentNum = mEtxtStudentNum.getText().toString();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.BtnStSchedule);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MyWebView.class);
startActivityForResult(myIntent, 0);
}
});
// TODO Auto-generated method stub
}
}
AntoniusActivity.java (Main):
Code:
package bas.sie.Antonius;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
public class AntoniusActivity extends TabActivity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Home.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("Home",
res.getDrawable(R.drawable.ic_tab_home))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, External.class);
spec = tabHost.newTabSpec("external").setIndicator("External",
res.getDrawable(R.drawable.ic_tab_external))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Contact.class);
spec = tabHost.newTabSpec("contact").setIndicator("Contact",
res.getDrawable(R.drawable.ic_tab_contact))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
In AntoniusActivity, the private class HelloWebViewClient is underlined with yellow (never used), specifically the bit "HelloWebViewClient".
It's also throwing FC's at startup, and errors in LogCat, but I'll post those later on, as it's 10.30 PM here, and school goes on
Thanks in advance,
bassie1995
No suggestions yet?
How you doin'? Greetings from my GT-I9000!

Android App Help

Ok so today i started my first Android Application in eclipse. I am VERY new at coding so bear with me. So i looked online on how to open a new screen when i press a button, but my question is, how do i open a different screen when i press a different button on the same page? Because you cant just copy and paste the same code for the first button because it gives you duplicate errors. Can anyone tell me what i can copy/paste or do to get my second button to go to a different screen? Thanks!
main.class (or java)
Code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MinecraftInfoProActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton(){
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, Mobs.class);
startActivity(intent);
}
});
}
}
main.xml
Code:
animals.class (or java) (this is my second page i wanna hook my second button up to)
Code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Animals extends Activity {
}
animals.xml
Code:
AndroidManifest.xml
Code:
I am trying to hook button2 in main.xml so that when clicked, it takes me to the animals.xml page. Any help is appreciated guys. Thanks
EDIT: Sorry about the smily faces in the code..im not sure what that is about.
Mod Edit: Moved to Q&A
Scoutamis said:
Ok so today i started my first Android Application in eclipse. I am VERY new at coding so bear with me. So i looked online on how to open a new screen when i press a button, but my question is, how do i open a different screen when i press a different button on the same page? Because you cant just copy and paste the same code for the first button because it gives you duplicate errors. Can anyone tell me what i can copy/paste or do to get my second button to go to a different screen? Thanks!
main.class (or java)
Code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MinecraftInfoProActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton(){
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, Mobs.class);
startActivity(intent);
}
});
}
}
main.xml
Code:
animals.class (or java) (this is my second page i wanna hook my second button up to)
Code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Animals extends Activity {
}
animals.xml
Code:
AndroidManifest.xml
Code:
I am trying to hook button2 in main.xml so that when clicked, it takes me to the animals.xml page. Any help is appreciated guys. Thanks
EDIT: Sorry about the smily faces in the code..im not sure what that is about.
Click to expand...
Click to collapse
Use CODE] /CODE] If you dont want smileys.
Sent from my GT-I5800 using XDA App
rubensollie said:
Use CODE] /CODE] If you dont want smileys.
Sent from my GT-I5800 using XDA App
Click to expand...
Click to collapse
I did so idk why it isn't working...anyway does ANYONE have any help?
Sent from my SAMSUNG-SGH-I727 using xda premium
Hope this helps ya out
In the main.xml, you can specify an event handler method for button2 by adding this in button2's xml declarations
Code:
android:onClick="yourMethodName"
That is a quick replacement for
Code:
button.setOnClickListener(new OnClickListener(){
public void yourMethodName(View view) {
Intent intent = new Intent(context, Mobs.class);
startActivity(intent);
}
});
Of course replace 'yourMethodName' with the method that you want to handle the click action. You can use this for button1 as well, just specify the onClick property in main.xml under button1. You need to create a method in your MinecraftInfoProActivity class that will be the handler for the button, so say you create a method called 'Button2Handler". To open up Animals (which you have to write the code to display the animals.xml) you could use this method.
Code:
public void Button2Handler(View view) {
Intent intent = new Intent(context, Animals.class);
startActivity(intent);
}
Hope this gives you what you need, as it's just a quick basic intro into it
regaw_leinad said:
Hope this helps ya out
In the main.xml, you can specify an event handler method for button2 by adding this in button2's xml declarations
Code:
android:onClick="yourMethodName"
That is a quick replacement for
Code:
button.setOnClickListener(new OnClickListener(){
public void yourMethodName(View view) {
Intent intent = new Intent(context, Mobs.class);
startActivity(intent);
}
});
Of course replace 'yourMethodName' with the method that you want to handle the click action. You can use this for button1 as well, just specify the onClick property in main.xml under button1. You need to create a method in your MinecraftInfoProActivity class that will be the handler for the button, so say you create a method called 'Button2Handler". To open up Animals (which you have to write the code to display the animals.xml) you could use this method.
Code:
public void Button2Handler(View view) {
Intent intent = new Intent(context, Animals.class);
startActivity(intent);
}
Hope this gives you what you need, as it's just a quick basic intro into it
Click to expand...
Click to collapse
Omg thank you that helped soooo much
Sent from my SAMSUNG-SGH-I727 using xda premium

[Q] Creating Translator App

Hi all, ive just started lookin up to Java and creating app, i know i should start and improove slowly, but i need quickly some helps with makin a Translator.apk using Bing API translator, cause google one are now only aviable if u pay them.
i saw a litle exemple where i downloaded the Jar file(which i put it in /libs and mine came out like this:
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
EditText in, out;
Button trns;
String inPut;
@Override
protected void onCreate() {
// TODO Auto-generated method stub
Translate.setClientId("hidden");
Translate.setClientSecret("hidden");
super.onCreate();
setContentView(R.layout.activity_main);
trns = (Button) findViewById(R.id.button1);
in = (EditText) findViewById(R.id.editText1);
out = (EditText) findViewById(R.id.editText2);
inPut = in.getText().toString();
trns.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String translatedText = Translate.execute(inPut, Language.GERMAN,
Language.FRENCH);
out.setText(translatedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
problaby its all wrong i know, thats why i need help
Guess i have to add Internet permission to the manifest(how ? and do i have to make some controll after?)
I studied at school some basic C# so im used to the grouBox which i didnt find with eclipse. What's the nearest thing with it where i can put a list of all selectable language to translate?
Hope somebody could help me, ty

Calling a fragment from another list type ListFragment

Hello everyone wanted to know if I can support, I have a project in which I have implemented a tablayout with 3 tabs, each tabs I have assigned a fragment, for the first tab I have a listfragment and show the list correctly, but selecting an element of the list want the switch to another fragment that will also display another list.
For example I have a list of categories, in the fragment of listfragment type in the first tab, but selecting an item from the list I want to show the items child of that category in another list, but in the same tab, reached the code, as managing that change, ie another fragment listfragment call type from listfragment.
File: TabCategoriasFragment.java
Code:
package com.gydsoluciones.grva.recetasperu;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by grva on 20/05/2016.
*/
public class TabCategoriasFragment extends ListFragment{
String[] categorias = {"Licores","Sopas","Pescados y Mariscos","Arroces","Ensaladas","Repostería","Salsas"};
Integer[] pics = {R.drawable.licores,R.drawable.sopas,R.drawable.pescadosmariscos,R.drawable.arroces,R.drawable.ensaladas,R.drawable.reposteria,R.drawable.salsas};
String[] descripcion = {
"Los mejores licores","Selección de las mejores sopas y caldos",
"Los mejores platos a base de pescados y mariscos","Arroz con pollo, Arroz chaufa, entre otros",
"Las mejores ensaladas frescas","Lo mejor para endulzar el momento","Las mejores salsas basadas en la variedad peruana"
};
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
CategriaListAdapter adapter = new CategriaListAdapter(getActivity(),categorias,pics,descripcion);
setListAdapter(adapter);
return inflater.inflate(R.layout.lista_categorias,container,false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String itemText = categorias[+position];
String url = "url web service";
ProgressDialog pDialog = new ProgressDialog(getContext());
pDialog.setMessage("Cargando...");
pDialog.show();
final JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("json_array_req",response.toString());
try {
String[] recetas = new String[response.length()];
for (int i = 0; i < response.length(); i++) {
JSONObject receta = (JSONObject) response.get(i);
recetas[i] = receta.getString("titurece");
}
}catch(JSONException e)
{
Log.d("json_array_req", e.toString());
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
VolleyLog.d("json_array_req","Error:" + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(req,"json_array_req");
pDialog.hide();
}
}

Categories

Resources