I have created a main screen to have 3 buttons. And one of them is to open another page that displays data from my database in a textview. It works perfectly on the emulator on my laptop but when I copy the files to my Nexus 7 that button crashes the application. All other buttons work fine.
here's the code, very basic:
viewFlare = (Button)findViewById(R.id.bViewFlare);
viewFlare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openViewFlare = new Intent("com.example.project.SQLFLAREVIEW");
startActivity(openViewFlare);
}
});
it calls this page:
public class SQLFlareView extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlflareview);
TextView textView = (TextView) findViewById(R.id.tvSQLFlareinfo);
Calms info = new Calms(this);
info.open();
//System.out.print("THIS IS THIS OPEN");
String data = info.getFlareData();
//System.out.print("ABOUT TO CLOSE");
info.close();
textView.setText(data);
}
}
If anyone would have any ideas I would be very appreciative, I have banged my head about this problem for a while.
Thanks in advance
Change this:
Intent openViewFlare = new Intent("com.example.project.SQLFLAREVIEW");
To this:
Intent openViewFlare = new Intent(name_of_current_class.this, SQLFLAREVIEW.class");
Related
I have just started trying out Android Studio, following the instructions on a video tutorial.
Here is my take on what the guy tells me but I keep getting the error message View is deprecated.
How do I fix this? Or what do I use instead?
Here is my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendButton0001 = (Button) findViewById(R.id.buttonSend0001);
EditText welcomeText = (EditText) findViewById(R.id.editTextWelcome);
sendButton0001.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
This is just a warning. I don't know why it shows up, but if it runs fine - don't take care.
I have connected my database through the phpmyadmin for android studio and it is working perfectly when use the code 10.0.3.2 in task.execute() function instead of localhost for genymotion emulator in adroid studio. But when i build the APK file for the same and transfer it to my android cell phone the app fails to read the database. What Can be done?
Here is the code for the main java class file that is being used.
Sorry for not providing it earlier
Java Class file code
public class MainActivity extends AppCompatActivity implements AsyncResponse, View.OnClickListener{
EditText etUsername, etPassword;
Button btnLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername= (EditText) findViewById(R.id.etUsername);
etPassword= (EditText) findViewById(R.id.etPassword);
btnLogin=(Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
}
#Override
public void processFinish(String result) {
if(result.equals("success")){
Intent in = new Intent(this,SubActivity.class);
startActivity(in);
}
else
{
Toast.makeText(this,"Login Failed", Toast.LENGTH_LONG).show();
}
}
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString() );
PostResponseAsyncTask task= new PostResponseAsyncTask(this, postData);
task.execute("http://10.0.3.2/client/login.php");
}
}
if i understand your problem correctly, you need to use the address of your computer in the network like: http://192.168.1.8
I'm looking to randomly start activities in android studio without repetition. I'd like to be able to start an activity randomly by clicking a button and in an 'if' statement. Whoever can help out that would be fantastic! Thank you.
ArrayList<Class> activities = new ArrayList<>();
#Override
public void onCreate(Bundle savedInstanceState) {
//your codes...
activities.add(History.class);
activities.add(Bookmark.class);
activities.add(Themes.class);
//your codes
}
public void openActivity(Class class_) {
Intent intent = new Intent(this, class_);
startActivity(intent);
}
public void openRandomActivity(){
Class that = activities.get(new Random().nextInt(activities.size()));
activities.remove(that); // after using, remove from list
openActivity(that);
}
Call openRandomActivity when a user click to button.
I am trying to make a button in one activity (SetupMenu) that, when pressed, puts an int into the intent and carries that over to the next activity (IntroActivity) where a textView will retrieve the int and display it.
Problem is, when the app runs and I get to the activity and press the button, the app crashes and my emulator tells me that "Unfortunately [my app] has stopped working."
I feel like I've tested every possible angle to get this to work. I should note that the button has worked fine, the textview has worked fine, everything else is working smoothly - I only run into issues when I try retrieving the intent and displaying it in textView. I tried passing through a String instead of an Int and also had issues (my string would not appear). Any pointers?
SetupMenu activity (here I put an int into my intent):
public class SetupMenu extends Activity {
public final static String extra_progress_key = "com.example.angelsanddemons.track_players";
public int track_players = 0;
public void to_intro(View view) {
Intent intent = new Intent(this, IntroActivity.class);
intent.putExtra(extra_progress_key, track_players);
startActivity(intent);
}
IntroActivity activity (here I try to retrieve the int from the intent):
public class IntroActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
textView.setText(temp);
setContentView(textView);
}
}
One problem is that you can't set a TextView's text to an int; you'll need to first convert it to an string. It's also not a good idea to be manipulating views before you've inflated them, so perhaps your onCreate() should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
setContentView(textView);
textView.setText(String.valueof(temp));
}
I see nothing that ensure that SetupMenu activity is created and in memory when IntroActivity is launched. To make sure, don't pass the variable, but the string itself and check if it work:
int temp = intent.getIntExtra("com.example.angelsanddemons.track_players", 0 );
Hi I am a total android newbie so my question might seem dumb.
I want to make a program that has two activities, one of them is TinyCalActivity1 and the other is TinyCalActivity2.
My intention was that I can make a button to switch between this activity,
package my.app.tinyCal;
public class TinyCalActivity1 extends Activity {
/** Called when the activity is first created. */
Button myButton;
EditText myEdit;
//Intent i = new Intent(TinyCalActivity1.this, TinyCalActivity2.class);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.Next);
myEdit = (EditText)findViewById(R.id.entry);
CharSequence thisNum = "1234565";
myEdit.setText(thisNum);
OnClickListener myListener = new OnClickListener(){
public void onClick(View v){
}
};
myButton.setOnClickListener(myListener);
}
}
and TinyCalActivity2.
However, when I write this line and run in my android AVD, it tells me that there's an error.
Intent i = new Intent(this, TinyCalActivity2.class);
There is no error hint in eclipse and I do not know why.
I create TinyCalActivity2.java as a separate file in the same folder as my TinyCalActivity1.java file. And I have registered TinyCalActivity2 in my AndroidManifest.xml.
I would really appreciate any help!
If you put your code
Intent i = new Intent(this, TinyCalActivity2.class);
in button click event, then this refers to button and not ACTIVITY. Thus, you need to mention it as
Intent i = new Intent(TinyCalActivity1.this, TinyCalActivity2.class);
This may resolve the issue
You can try this way.
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent i = new Intent(this, TinyCalActivity2.class);
finish();
}
};