I am trying to read file from assets in android. I want every button to print a paragraph, for example the first button prints the first paragraph and the second button print the second paragraph.
I tried with the code below but its not working.
I get this error message:
03-03 18:40:17.800: E/AndroidRuntime(977): FATAL EXCEPTION: main
03-03 18:40:17.800: E/AndroidRuntime(977): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.hajjapp.AfterHajj }
03-03 18:40:17.800: E/AndroidRuntime(977): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
03-03 18:40:17.800: E/AndroidRuntime(977): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
03-03 18:40:17.800: E/AndroidRuntime(977): at android.app.Activity.startActivityForResult(Activity.java:3370) –
Can anyone tell me where the problem is?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Dialog;
import android.content.res.AssetManager;
public class BeforeHajj extends ActionBarActivity {
Button whatHajj;
TextView text;
Button hajjTime;
String before;
String [] s;
String timeHajj="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_before_hajj);
whatHajj = (Button)findViewById(R.id.whatisHajj);
hajjTime = (Button)findViewById(R.id.hajjTime);
text = (TextView)findViewById(R.id.text);
whatHajj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try
{
AssetManager assetmanager=getAssets();
InputStream input=assetmanager.open("beforehaj.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(input));
String st="";
StringBuilder sb=new StringBuilder();
while((st=br.readLine())!=null)
{
sb.append(st);
before+=sb.toString();
s=before.split("*");
}
text.setText(before);
}
catch(IOException ep) {
text.append(ep.toString());
}
}
});
hajjTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// custom dialog
try
{
AssetManager assetmanager=getAssets();
InputStream input=assetmanager.open("beforehaj.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(input));
String st="";
StringBuilder sb=new StringBuilder();
while((st=br.readLine())!=null){
sb.append(st);
before+=sb.toString();
s=before.split("*");
}
}
catch(IOException ep) {
text.append(ep.toString());
}
final Dialog dialog = new Dialog(BeforeHajj.this);
dialog.setContentView(R.layout.guide_dialog);
dialog.setTitle("Hajj Time");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.dialog_text);
text.append(s[0]);
ImageView image = (ImageView) dialog.findViewById(R.id.dialog_image);
image.setImageResource(R.drawable.dolheja);
Button dialogButton = (Button) dialog.findViewById(R.id.dialog_button);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.before_hajj, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Somewhere, you are trying to start an activity that is supposed to be implemented in the Java class com.example.hajjapp.AfterHajj. The error message is telling you that there is no <activity> element in the manifest for this class. Also note that the code you pasted into your question is from a Java class named BeforeHajj.
Use new line character to split the paragraphs. All paragraphs start with a new line, so use
before.split("\\\n");
This shall do the job for you.
Use escape symbols to make the '*' be recognized by split, e.g. s.split("\\*");
Related
I'm learning how to program in Java for Android and my program keeps crashing. It's suppoesed to convert from Celsius to Fahrenheit.
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener
{
public static final int MY_CODE=2;
EditText temp;
RadioButton converttoF,converttoC,selectedtype;
RadioGroup conversion;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conversion = (RadioGroup)findViewById(R.id.conversion);
conversion.setOnCheckedChangeListener(this);
converttoF = (RadioButton)findViewById(R.id.converttoF);
converttoC = (RadioButton)findViewById(R.id.converttoC);
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
selectedtype = (RadioButton) findViewById(checkedId);
Intent i = new Intent(this, Activity4Result.class);
i.putExtra("temper", temp.getText().toString());
if(selectedtype.equals(converttoC))
{
Toast.makeText(this,"C chosen", Toast.LENGTH_SHORT).show();
i.putExtra("type", 'c');
startActivityForResult(i, MY_CODE);
}
else
{
Toast.makeText(this,"F chosen", Toast.LENGTH_SHORT).show();
i.putExtra("type", 'f');
startActivityForResult(i, MY_CODE);
}
}
}
My guess is that the problem is happening in the "onCheckedChanged", because it crashes when I push any RadioButton.
I assume (It would be better if you post the crash log along with your question, so that we can help you better and stop assuming) you are getting a null pointer exception on the following code:
i.putExtra("temper", temp.getText().toString());
In your code you declared temp, but it is never initialised. You need to initialise temp before using it.
After asking a similar question this morning, I did more research and found (I think) a way to save/load my ArrayList by saving it to SharedPreferences (After converting it to a Json String).
The problem is, I don't know where to put my saveListe() and my loadListe().
I don't get any error, but when restarting the application, the list is still the default one (It has 1 game in it).
Those 2 methods and my list are in a ListeJeux Class.
I tried using loadListe() in my mainActivity, and I used saveListe() in another activity (that add a game to the list for test purposes) just after adding a game.
If I launch the other activity, it adds a game to the list (this part is working), but if I restart the application, the list still contains 1 game only (default). What is the problem?
ListeJeux (contains save and load methods + the default list):
package com.example.jouons;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
#SuppressWarnings("deprecation")
public class ListeJeux extends ActionBarActivity {
// By default, the list contains one game, if the listView show more than
// one, then addJeu() works, if it still shows more than one game after a
// restart, then the save/load works (not working for now).
static Jeu jeu1 = new Jeu("Ballon fou", "moyens grands", "intérieur");
static ArrayList<Jeu> jeux = new ArrayList<>(Arrays.asList(jeu1));
// This is only to test.
static void addJeu() {
jeux.add(new Jeu("Ballon fou", "moyens grands", "intérieur"));
}
// Takes "jeux" (this is where the problem happens, I think), and makes it a
// Json String. Then store it in SharedPreference.
static public void saveListe(Context context) {
Gson gson = new Gson();
String listeJson = gson.toJson(jeux);
SharedPreferences prefs = context.getSharedPreferences("listePrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("listeEditor", listeJson);
editor.commit();
}
// Takes the contain of the SharedPreference (Json String) and reverts it to
// an ArrayList. Then returns it.
static public ArrayList<Jeu> loadListe(Context context) {
Gson gson = new Gson();
SharedPreferences prefs = context.getSharedPreferences("listePrefs", MODE_PRIVATE);
String extractedJson = prefs.getString("listeEditor", "");
Type type = new TypeToken<ArrayList<Jeu>>() {
}.getType();
ArrayList<Jeu> jeux = gson.fromJson(extractedJson, type);
return jeux;
}
}
The MainActivity (in which I use the loadList() method):
package com.example.jouons;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
#SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListeJeux.loadListe(this);
setupTrouvezButton();
setupRechercherButton();
setupListeButton();
}
private void setupListeButton() {
Button listeButton = (Button) findViewById(R.id.btnMainListe);
listeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ListeActivity.class));
}
});
}
private void setupTrouvezButton() {
Button trouvezButton = (Button) findViewById(R.id.btnMainTrouvez);
trouvezButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TrouvezActivity.class));
}
});
}
private void setupRechercherButton() {
Button rechercherButton = (Button) findViewById(R.id.btnMainRechercher);
rechercherButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, RechercherActivity.class));
}
});
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And the other activity (that add a game to the list then save it):
package com.example.jouons;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
#SuppressWarnings("deprecation")
public class RechercherActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechercher);
ListeJeux.addJeu();
ListeJeux.saveListe(this);
setupRetourButton();
}
private void setupRetourButton() {
Button retourButton = (Button) findViewById(R.id.btnRechercherRetour);
retourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.rechercher, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
If you want to save a arraylist to the shared prefrence, you have to serialize the arraylist before you saving. I would prefer saving a serialized object to the sdcard instead of saving it to the shared prefrence, if it is not a confidantial information. if it is a cache file, i would recomend you save it to the app cache directory.
Serialize an object
public class SomeClassName implements Serializable{
private ArrayList<SomeObject> arrayList;
public void setObject(ArrayList<SomeObject> list){
arrayList = list;
}
public ArrayList<SomeObject> getObject(){
return arrayList;
}
}
ActionBarActivity although it is deprecated, it extends Activity, so you should be able to override onCreate, onResume and onPause to call your methods to load and save. For example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechercher);
setupRetourButton();
}
#Override
protected void onResume()
{
super.onResume();
ArrayList<Jeu> loaded = ListeJeux.loadListe(this);
for (Jeu j: loaded)
{
// This method to add doesn't exist and this whole loop should
// be refactored into the loadListe method.
ListeJeux.addJue(j)
}
// This size() method doesn't exist either and could also be subsumed into loadList
if (ListeJeux.size() == 0)
{
ListeJeux.addJeu();
ListeJeux.saveListe(this);
}
}
#Override
protected void onPause()
{
// Save the list as the app may stop any time after a pause.
ListeJeux.saveListe(this);
super.onPause();
}
Personally, I would refactor the code a little so the save and load methods on the list (ListeJeux), actually update the internal list. As you can see from the diagram at the top of this Android Documentation, you really only need to load in onResume as it should always be called before the Activity is displayed.
package com.example.desktop.mirror;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.MenuItem;
import android.view.MenuItem;
import android.widget.EditText;
import java.util.Scanner;
public class MainActivity extends ActionBarActivity {
public EditText a;
public String entereda;
public EditText b;
public String enteredb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OnClick(View v){
a = (EditText) findViewById(R.id.first_text);
b = (EditText) findViewById(R.id.second_text);
if(boolean entereda){
Intent i = new (this, reverseactivity.class);
startActivity(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
// 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 = menu.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(menu);
}
I'm new to both Java and Android, so I can not be more specific.
My question might be a silly one, since it's a simple one to be implemented in Java, but coming to implementation on Android, i'm struggling a lot with the code.
Try out this minimal code (2 lines, excluding the Sysyem.out.println()) :
// The big works ;)
StringBuffer buffer = new StringBuffer("Test!");
buffer.reverse();
// Test it out
System.out.println(buffer); // .toString());
Output:
!tseT
Required import:
java.lang.StringBuffer
public void OnClick (View v){
a = (EditText) findViewById(R.id.first_text);
b = (EditText) findViewById(R.id.second_text);
StringBuilder s = new StringBuilder(100);
String entereda = a.getText().toString();
int str_len = entereda.length();
for (int i = (str_len - 1); i >= 0; i--) {
System.out.print(entereda.charAt(i));
s.append(entereda.charAt(i));
}
Not very familiar with android either, but to reverse strings:
public static String reverse(String toReverse) {
Stack<Character> letters = new Stack<Character>();
for(char c:toReverse.toCharArray()) {
letters.push(c);
}
StringBuilder sb = new StringBuilder();
while(!letters.empty()) {
sb.append(letters.pop());
}
return sb.toString();
}
And just have the result show up using a Thread and setting the result to the value of where you want it to go.
Java isn't different from Android, just Android has built-in GUI capabilities.
And how do you get the code to show up in java?
Hi I am developing an android application, nothing fancy just something small for college. Where I use R I recieve an error on the final bit. example R.id.mediAware
Any help would be greatly appreciated.
Here is my code if that helps:
package com.example.medicalapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.medicalapp.R;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img1 = (ImageView) findViewById(R.id.mediAware);
ImageView splash_image2 = (ImageView) findViewById(R.id.mediAware2);
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fadein);
Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fadein2);
fade2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd (Animation animation) {
Intent intent = new Intent(new Intent(MainActivity.this, MenuActivity.class));
startActivity(intent);
}
public void onAnimationStart(Animation animation){
}
public void onAnimationRepeat(Animation animation){
}
});
splash_image.startAnimation(fade1);
splash_image2.startAnimation(fade2);
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I figured out my problem. I was missing the R.java file. To add the file i simply created a folder in src called gen and then cleaned the project, this created the R.java file and removed my errors, appreciate the help that was given
E/AndroidRuntime(15518): FATAL EXCEPTION: main
E/AndroidRuntime(15518): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cydeon.plasmamodz/com.cydeon.plasmamodz.Boots}: java.lang.NullPointerException
E/AndroidRuntime(15518): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
E/AndroidRuntime(15518): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
E/AndroidRuntime(15518): at android.app.ActivityThread.access$600(ActivityThread.java:154)
E/AndroidRuntime(15518): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1248)
E/AndroidRuntime(15518): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(15518): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(15518): at android.app.ActivityThread.main(ActivityThread.java:5235)
E/AndroidRuntime(15518): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15518): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(15518): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
E/AndroidRuntime(15518): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
E/AndroidRuntime(15518): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(15518): Caused by: java.lang.NullPointerException
E/AndroidRuntime(15518): at android.app.Activity.findViewById(Activity.java:1839)
E/AndroidRuntime(15518): at com.cydeon.plasmamodz.Boots.<init>(Boots.java:47)
E/AndroidRuntime(15518): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(15518): at java.lang.Class.newInstance(Class.java:1319)
E/AndroidRuntime(15518): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
E/AndroidRuntime(15518): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
E/AndroidRuntime(15518): ... 11 more
The topic is confusing. Let me explain. In my app, I have over 70 buttons just for one section. I have many different sections. Now, each button uses the same layout, so each button starts the same activity, that way I don't create 70 seperate classes. Now, in this new class, it must show an image and download a file(when a certain button is clicked) corresponding to the bugton that was picked. So, basically, I need some way to identify those buttons in this other class, and say "if button x was pressed, display image y, if button y was pressed, display image x. Similarly, if button x was pressed, download file y, if button y was pressed, downloa d file x, ect. I just have no idea how to tell which button was pressed in that previous class. I really need help with this. I've been working for 2 weeks and this has put me to a stop, and I cannot find an answer. This isn't really code dependent, and I'll be using this method multiple times throughout the app, so I don't think posting any code is needed. Again, to reiterate my question, multiple buttons start the same activity. In that activity, I need to do stuff according to the button that was pressed in the previous class. Thanks. :)
Edit: Here's some code...
BootFragment.java:
package com.cydeon.plasmamodz;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.stericson.RootTools.RootTools;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.CommandCapture;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class BootFragment extends Fragment {{
//Defining File Directory
File directory = new File(Environment.getExternalStorageDirectory() + "/plasma/boot");
if(directory.exists() && directory.isDirectory()){
//Do nothing. Directory is existent
}else{
//Directory does not exist. Make directory (First time app users)
directory.mkdirs();
}
File bkup = new File(Environment.getExternalStorageDirectory() + "/plasma/boot/bkup");
if(bkup.exists() && bkup.isDirectory()){
//Do nothing. Directory is existent
}else{
//Directory does not exist. Make directory (First time app users)
bkup.mkdirs();
}}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.boot_frag,
container, false);
Button Ablue = (Button) view.findViewById(R.id.bABlue);
Button AblueR = (Button) view.findViewById(R.id.bABlueR);
Button Abokeh = (Button) view.findViewById(R.id.bAbokeh);
Ablue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent b = new Intent(getActivity(), Boots.class);
b.putExtra("Dragon", R.id.bABlue);
BootFragment.this.startActivity(b);
}
});
AblueR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent c = new Intent(getActivity(), Boots.class);
c.putExtra("Xbox", R.id.bABlueR);
BootFragment.this.startActivity(c);
}
});
Abokeh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent d = new Intent(getActivity(), Boots.class);
d.putExtra("GameB", R.id.bAbokeh);
BootFragment.this.startActivity(d);
}
});
return view;
}}
Boots.java:
package com.cydeon.plasmamodz;
import com.stericson.RootTools.*;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.CommandCapture;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.concurrent.TimeoutException;
import com.cydeon.plasmamodz.R;
import android.app.ActionBar;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
//Class for Boot Animation Blue Kindle
public class Boots extends Activity {
public static String TAG = "Boots";
Process process;
private class DownloadFile extends AsyncTask<String, Integer, String>{
#Override
protected String doInBackground(String... sURL) {
try{
URL url = new URL(sURL[0]);
URLConnection connection = url.openConnection();
connection.connect();
//Shows 0-100% progress bar
int fileLength = connection.getContentLength();
//Download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/plasma/boot/b..ootanimation.zip");
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
//Publish the Progress
publishProgress((int) (total * 100/fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
#Override
protected void onProgressUpdate(Integer... progress){
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]);
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
mProgressDialog.dismiss();
Context context = getApplicationContext();
CharSequence text = "Installing. Please Wait";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
if (RootTools.isBusyboxAvailable()){
RootTools.remount("/system", "rw");
CommandCapture command = new CommandCapture(0, "su", "sh /sdcard/plasma/scripts/boots.sh");
try {
RootTools.getShell(true).add(command).waitForFinish();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (RootDeniedException e) {
e.printStackTrace();
}
} else {
RootTools.offerBusyBox(Boots.this);
}
}
}
ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boots);
ActionBar actionBar = getActionBar();
actionBar.hide();
ImageView img = (ImageView) findViewById(R.id.iv2);
img.setImageResource(R.drawable.boot1);
Button install = (Button) findViewById(R.id.bAInstall);
Button rtrn = (Button) findViewById(R.id.bAReturn);
mProgressDialog = new ProgressDialog(Boots.this);
mProgressDialog.setMessage("Downloading..." );
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
install.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
TextView creator = (TextView) findViewById(R.id.tvCreate);
Intent bootI = getIntent();
int dball = getIntent().getExtras().getInt("Dragon", -1);
int xbox = getIntent().getExtras().getInt("Xbox", -1);
int GameB = getIntent().getExtras().getInt("GameB", 1);
if (dball != -1){
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http:\\correspondingurl");
creator.setText("Dragon Ball");
}if (xbox != -1){
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http:\\correspondingurl");
creator.setText("Xbox");
}if (GameB != -1){
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http:\\correspondingurl");
creator.setText("GameBoy");
}
}
}
);
rtrn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
}
});
}
}
I have tried many different methods. This is my latest attempt. None of them have worked.
Cat log: Look at the top...
Pass the required information to the new activity in the Intent, via Intent.putExtra()
(see http://developer.android.com/reference/android/content/Intent.html)
You may try assigning each button a unique name/id. When the Button is clicked, retrieve its name and pass it to the new activity using Intent.putExtra(). In new activity, once you have this information, you can perform required function accordingly.
Sample Code :
In the xml for Button, do this
android:onClick="OnButtonClick"
In code of First Activity, defineOnButtonClick as :
public void OnButtonClick(View v) {
switch (v.getId()) {
case R.id.button1:
doSomething1(); //Intent.putExtra(ButtonID)
break;
case R.id.button2:
doSomething2();
break;
}
}
In second Activity, retrieve this ID and have a similar switch-case to perform actions depending on Id of Button.
Hope this makes sense!
In Your fragment class, do the following :
public class BootFragment extends Fragment implements android.view.View.OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Button Ablue = (Button) view.findViewById(R.id.bABlue);
Button AblueR = (Button) view.findViewById(R.id.bABlueR);
Button Abokeh = (Button) view.findViewById(R.id.bAbokeh);
Ablue.setOnClickListener(this);
AblueR.setOnClickListener(this);
Abokeh.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int btnId = v.getId();
Intent d = new Intent(getActivity(), Boots.class);
d.putExtra("BUTTON_ID", btnId);
startActivity(d);
}
}
In Boots.java, do the following:
public class Boots extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
int btnId = getIntent().getExtras().getInt("BUTTON_ID", -1);
switch(btnId){
case: R.id.your_btn_id_1:
// logic for your button press
break;
case: R.id.your_btn_id_2:
// logic for your button press
break;
case: R.id.your_btn_id_n:
// logic for your button press
break;
}
}
}
Note: You should provide unique id to each button in layout xml file