I am joining two activity through intent
package com.smartcodeone.newapp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
public static final String STRING_VAR = "com.smartcodeone.newapp1.HELLO_WORLD";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnMsg = (Button) findViewById(R.id.btnMsg);
btnMsg.setOnClickListener(new View.OnClickListener(){
//when user click's this function will be called
public void onClick(View v){
Intent intentvar = new Intent(getApplicationContext(),Main2Activity.class);
intentvar.putExtra(STRING_VAR,"Hello World"); //this is used to pass data to next intent
startActivity(intentvar);
}
});
}
private int findViewId(int btnMsg) {
return 0;
}
#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 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);
}
#Override
public android.support.v4.app.FragmentManager getSupportFragmentManager() {
return null;
}
}
It might have to do with Android Studio. Try cleaning your project and then rebuilding. If that doesn't work go to File -> Invalidate Caches/Restart ...
I get those issues sometimes as well. Let me know if that works. I tried your code and it works fine.
The issue seems to be that you defined a method named: findViewById(int button) that always return 0.
Use the Activity method instead of your own:
this.findViewById(int resourceId)
Good luck!
It seems you are implementing your own findviewbyid(). I don't know if you intend to do so.
Try removing
private int findviewbyid(int btnMsg) {
}
The ActionBarActivity's findviewbyid should resolve your button from your layout file.
Related
I am trying to build an app that displays a second activity when the button is pushed. It also changes the text of a TextView in the second activity (id:baconTextId) based on text entered in the first activity's EditText (id:ApplesInput).
Activity 1 : Apples.java
Activity 2: Bacon.java
On both buttons in the xml I put
android:onClick = "Thismethodiscalledonclick"
So that I don't need to add listeners.
I was following this tutorial on intents, but got an error. Android Studio shows no errors, but when I push the button "ApplesInput" w/ id of "ApplesInput" my phone says, "unfortunately Intent Example has stopped."
After pushing this button, I should get a screen like the following:
First activity: Apples.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.*;
import android.widget.*;
public class Apples extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apples);
}
public void Thismethodiscalledonclick(View v){
Intent i = new Intent(this, Bacon.class);
final EditText ApplesInput = (EditText) findViewById(R.id.ApplesInput); //Named var and id same for simplicity
String usersmessage = ApplesInput.getText().toString(); //Whatever user types
i.putExtra("applesMessageKey", usersmessage); //Parameters == ("What do you want to call this", What piece of information?)
startActivity(i); //Starts intent stuff
}
#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_apples, 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);
}
}
Second Activity: Bacon.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
public class Bacon extends AppCompatActivity {
final TextView BaconText = (TextView) findViewById(R.id.BaconTextid);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bacon);
Bundle applesData = getIntent().getExtras(); //Call intent and store the extras in applesdata
if(applesData == null){
BaconText.setText("Must type something, sending you back...");
Thismethodiscalledonclick(null); //Take them back to first screen
return;
}
String applesDataReceived = applesData.getString("appleMessageKey");
BaconText.setText(applesDataReceived);
}
public void Thismethodiscalledonclick(View v){
Intent i = new Intent(this, Apples.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_bacon, 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);
}
}
Some of the logcat error filtered:
android.provider.Settings$SettingNotFoundException: accessibility_enabled
at android.provider.Settings$Secure.getIntForUser(Settings.java:3163)
at android.provider.Settings$Secure.getInt(Settings.java:3148)
at com.android.systemui.power.PowerUI$1.onReceive(PowerUI.java:386)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:774)
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:5272)
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:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
at dalvik.system.NativeStart.main(Native Method)
Feel free to ask me for anymore code or resources if you think it is needed in the problem solving process.
Try this:
private TextView BaconText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bacon);
BaconText = (TextView) findViewById(R.id.baconText);
Bundle applesData = getIntent().getExtras(); //Call intent and store the extras in applesdata
if(applesData == null){
BaconText.setText("Must type something, sending you back...");
Thismethodiscalledonclick(null); //Take them back to first screen
return;
}
String applesDataReceived = applesData.getString("applesMessageKey");
BaconText.setText(applesDataReceived);
}
public void Thismethodiscalledonclick(View v){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
Call findViewById() in onCreate().
And name putExtra in ApplesActivity is "applesMessageKey" and name getString in BaconActivity is "appleMessageKey". They not same :D
It's the same code in the tutorial but when I tried the code. It gives me the above error.
package com.example.rishav_pc.fragments;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListner {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// This gets called by the top fragment when the user clicks the button
#Override
public void createMeme(String top, String bottom) {
BottomPictureFragment bottomFragment = (BottomPictureFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
bottomFragment.setMemeText(top, bottom);
}
#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 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);
}
}
There is one more error
Error:Execution failed for task ':app:compileDebugJava'.
Compilation failed; see the compiler error output for details.
In your BottomPictureFragment change the import of the extended class Fragment to be import android.support.v4.app.Fragment; . It seems that by you it is something else. You should have something like this :
import android.support.v4.app.Fragment;
...
public class BottomPictureFragment extends Fragment { ... }
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
I am new to android programming on eclipse and just doing it as a hobby...I have made and run 2 apps successfully and so their is no question of me not knowing how to run the app...i have restarted my pc but the problem sill persists..my 2 apps are still working on pressing the run button and i am using a device to run them(not an AVD) but the third app is not working...in fact their is no response..their are no errors in code but 3 lint warnings..the program is very simple(taking 2 numbers from user and displaying the sum)...Please help.MY code is as following:
package com.example.app3;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivityapp3 extends ActionBarActivity {
int num1,num2,sum;
Button button1;
EditText textf1,textf2;
TextView textv3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activityapp3);
textf1=(EditText)this.findViewById(R.id.editText1) ;
textf2=(EditText)this.findViewById(R.id.editText2);
textv3=(TextView)this.findViewById(R.id.textView3);
button1=(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
num1 = Integer.parseInt(textf1.getText().toString());
num2 = Integer.parseInt(textf2.getText().toString());
sum = num1 + num2;
textv3.setText("sum" + "");
}
});
}
#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_activityapp3, 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);
}
}
Would really need to see the output of logcat. You can find it in the DDMS tabs of Eclipse.
Anyway, without more information I would ask: did you remember changing the activity name in AndroidManifest?
I am new to android studio and I am reading the tutorials on i-programmer located here (http://www.i-programmer.info/programming/android/5914-android-adventures-activity-and-ui.html?start=2)
The two objects in the environment are a button and a large text widget instanced
in android studio's xml designer.
The problem: A method for setting text referenced in the tutorial is showing this error message when I try to run the code: error: cannot find symbol method setText(String)
And this error shows up in the text editor: Cannot resolve method 'setText(java.lang.String)'
Provided Source Code:
package com.example.helloworld1.helloworld1;
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;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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);
}
public void onButtonClick(View v){
Button button=(Button) v;
v.setText("I've Been Clicked!"); // This is where the error happens
}
}
I think you meant to use button instead of v.
button.setText("I've Been Clicked!");