Executing code in 2end activity - Android Studio

Hi .
I'm newbie so I need your help in some issue
I'm coding an app that has 2 activity .
The problem is I want Some codes to be execute right after user goes to 2end activity .
So I write the codes in Oncreate function . but in test when user goes to 2end activity he will see just empty design of activity . like I didn't write any code for that :/
Please help me what should I do .
p.n : I want 2end activity to show the inbox content to user and I'm sure that my codes is correct .
sry for my bad english .

You need to display all your codes.

hiphop12ism said:
You need to display all your codes.
Click to expand...
Click to collapse
Activity A
Code:
public class A extends Activity implements View.OnClickListener {
Button inbox,draft,sent,call;
TextView lblmsg,lblNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
inbox = (Button) findViewById(R.id.btn1);
sent = (Button) findViewById(R.id.btn2);
draft = (Button) findViewById(R.id.btn3);
call = (Button) findViewById(R.id.btn4);
inbox.setOnClickListener(this);
draft.setOnClickListener(this);
call.setOnClickListener(this);
sent.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == inbox) {
Intent i;
i = new Intent(this, B.class);
startActivity(i);
}
}
Activity B
Code:
public class B extends AppCompatActivity {
ListView lv1;
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
lv1=(ListView)findViewById(R.id.lv);
Uri InboxUri= Uri.parse("Content://sms/inbox");
String [] C= new String[] { "_id", "address", "body" };
ContentResolver cr=getContentResolver();
Cursor D = cr.query(InboxUri, C, null, null, null);
adapter = new SimpleCursorAdapter(
this,
R.layout.row,
D,
new String[]{"body","address"},
new int[]{R.id.lblmsg,R.id.lblNumber});
lv1.setAdapter(adapter);
}
and I forgot to say Android studio detecting no error

Related

Onclock property

Can set an oncick listener via this property or not?
I have tried this: androidnClick="@string/str_onclick_searchbt"
where str_onclick_searchbt = "onClickSearch" corresponding to a function onClickSearch(View view).
But it does not seem to be working - my app seems to crash.
If you can't do this then what is the onclick property for?
Change to anonymous onclick listener
I have changed my code to implement the onclick listener on onCreate rather than through the onclick button property.
But still my app seems to crash due to the highlighted code. If I comment it out my app starts up as expected but does not respond to button clicks.
So what I am doing wrong?
Code:
public class HexapodControls extends Activity
{
BluetoothAdapter m_BTAdapter;
BroadcastReceiver m_BTReceiver;
ArrayAdapter<String> m_BTArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
final Button buttonConnectBT = (Button)findViewById(id.button_connectbt);
final Button buttonDisconnectBT = (Button)findViewById(id.button_disconnectbt);
final Button buttonSearchBT = (Button)findViewById(id.button_searchbt);
final Button spinnerBT = (Button)findViewById(id.spinner_bluetooth);
final AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("In onCreate(...)");
alertDialog.setMessage("Search button clicked...");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", (DialogInterface.OnClickListener) null);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hexapod_controls);
getBluetoothDevices();
[B]buttonSearchBT.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
spinnerBT.setEnabled(false);
buttonConnectBT.setEnabled(false);
buttonDisconnectBT.setEnabled(false);
alertDialog.show();
if (m_BTAdapter.isDiscovering())
m_BTAdapter.cancelDiscovery();
m_BTAdapter.startDiscovery();
}
});[/B]
/*
buttonConnectBT.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view)
{
buttonConnectBT.setEnabled(false);
buttonDisconnectBT.setEnabled(true);
}
});
buttonDisconnectBT.setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View view)
{
buttonConnectBT.setEnabled(true);
buttonDisconnectBT.setEnabled(false);
}
});
*/
}

user input from a edittext to another activity as normal textview with buttonclick

Hello, I need help for a little project
I have a TextView and a Button in the MainActivity, and an EditText and Button in Activity2 which returns back to MainActivity.
I can switch to the other Activity but I can't send the input of the user in Activity2 back as TextView.
I think there is a problem with the method addListenerOnButton but I'm not sure...
MainActivity:
Code:
public class MainActivity extends Activity {
public TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultText = (TextView) findViewById(R.id.resultText);
// resultText.setText(getIntent().getStringExtra(""));
}
public void onStartButtonClick(View view){
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
Activity2:
Code:
public class Activity2 extends Activity {
public EditText editText;
Button zuruckButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
addListenerOnButton();
editText = (EditText) findViewById(R.id.editText);
}
public void addListenerOnButton(){
zuruckButton = (Button) findViewById(R.id.zuruckButton);
zuruckButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
EditText editText = (EditText) findViewById(R.id.editText);
String text = editText.getText().toString();
Intent intent2 = new Intent(view.getContext(), MainActivity.class);
intent2.putExtra("mein Text", text);
startActivity(intent2);
}
});
}
}
mina2005 said:
Hello, I need help for a little project
I have a TextView and a Button in the MainActivity, and an EditText and Button in Activity2 which returns back to MainActivity.
I can switch to the other Activity but I can't send the input of the user in Activity2 back as TextView.
I think there is a problem with the method addListenerOnButton but I'm not sure...
MainActivity:
Code:
public class MainActivity extends Activity {
public TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultText = (TextView) findViewById(R.id.resultText);
// resultText.setText(getIntent().getStringExtra(""));
}
public void onStartButtonClick(View view){
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
Activity2:
Code:
public class Activity2 extends Activity {
public EditText editText;
Button zuruckButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
addListenerOnButton();
editText = (EditText) findViewById(R.id.editText);
}
public void addListenerOnButton(){
zuruckButton = (Button) findViewById(R.id.zuruckButton);
zuruckButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
EditText editText = (EditText) findViewById(R.id.editText);
String text = editText.getText().toString();
Intent intent2 = new Intent(view.getContext(), MainActivity.class);
intent2.putExtra("mein Text", text);
startActivity(intent2);
}
});
}
}
Click to expand...
Click to collapse
Where your
Code:
editText = (EditText) findViewById(R.id.editText);
is, you can delete this as you are referencing it in your button click method. Unless you are using it for anything else then this can be removed.
Also where
Code:
// resultText.setText(getIntent().getStringExtra(""));
is, this can be uncommented and at the end in brackets of
Code:
getStringExtra("")
is, you should put the extra string you stated as
Code:
intent2.putExtra("mein Text", text);
"mein Text" and put this in the empty
Code:
getStringExtra("")
.
The getStringExtra is is getting the value you put in as text with the key name of "mein Text". So when you call getStringExtra it's saying we will get the value of a string by the name of "mein Text" to identify it and return it.
If this was blank it would not have a name to reference a value to.
Hope this helped i'm new to this as well and hope this is OK for you.
yesss
philnewby2012 said:
Where your
Code:
editText = (EditText) findViewById(R.id.editText);
is, you can delete this as you are referencing it in your button click method. Unless you are using it for anything else then this can be removed.
Also where
Code:
// resultText.setText(getIntent().getStringExtra(""));
is, this can be uncommented and at the end in brackets of
Code:
getStringExtra("")
is, you should put the extra string you stated as
Code:
intent2.putExtra("mein Text", text);
"mein Text" and put this in the empty
Code:
getStringExtra("")
.
The getStringExtra is is getting the value you put in as text with the key name of "mein Text". So when you call getStringExtra it's saying we will get the value of a string by the name of "mein Text" to identify it and return it.
If this was blank it would not have a name to reference a value to.
Hope this helped i'm new to this as well and hope this is OK for you.
Click to expand...
Click to collapse
thank youuuuu that was what i needed! :good:

Trouble with implementing In App Billing into my app...

So I was programming in some In app Billing code for my Android app using Android Studio. I was following an Android tutorial called 'Preparing your In-app Billing Application' at the Android Developer website (not allowed to give a link because I'm a new user). All was going well, until I get to the part where I have to paste their code into my code. Check out their code put into mine (I left the base64EncodedPublicKey empty so nobody would steal it):
Code:
public class MainActivity extends ActionBarActivity {
//Add other java files to the main class
private JokeBook mJokeBook = new JokeBook();
private ColorWheel mColorWheel = new ColorWheel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare View variables
final TextView jokeLabel = (TextView) findViewById(R.id.jokeTextView);
final Button showJokeButton = (Button) findViewById(R.id.showJokeButton);
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
final Button moreJokes = (Button) findViewById(R.id.moreJokes);
//On Click
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String joke = mJokeBook.getJoke();
//Update label with fact
jokeLabel.setText(joke);
int color = mColorWheel.getColor();
relativeLayout.setBackgroundColor(color);
showJokeButton.setTextColor(color);
moreJokes.setTextColor(color);
}
};
showJokeButton.setOnClickListener(listener);
goToTwitter();
goToFacebook();
goToExxellerate();
IabHelper mHelper;
@Override
public void [U]onCreate(Bundle savedInstanceState)[/U] {
// ...
String base64EncodedPublicKey = "";
// compute your public key and store it in base64EncodedPublicKey
[B]mHelper = new IabHelper(this, base64EncodedPublicKey);[/B]
}
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
// Oh noes, there was a problem.
[I]Log.d(TAG, "Problem setting up In-app Billing: " + result);[/I]
}
// Hooray, IAB is fully set up!
}
});
@Override
public void [U]onDestroy()[/U] {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
}
The code that they gave me had many errors when pasted into Android Studio, which were:
TAG has private access in 'android.support.v4.app.FragmentActivity' (Italicized text)
Annotations are not allowed here (@Override)
; expected (Underlined text)
IabHelper (android.view.View.onClickListener, String) in IabHelper cannot be applied to (com.exxellerate.joketeller.MainActivity, String) (Bolded Text)
Any help with fixing there errors? I'm a beginner at code and very confused that the code they gave me was wrong...

Take input from edittext, click to save and create new textview

Hello,
this time iI'm working on an app where you can write something and click to the save button.
The important thing is that if you click on the save button than you have a new created textview in the other activity
the class where you write a notice and click on save button
Code:
public class NewNoticeActivity extends AppCompatActivity {
Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_notice);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void onCancelClick(View view){
Intent intentNew = new Intent(view.getContext(), NewActivity.class);
startActivity(intentNew);
}
public void onSaveClick(View view){
saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
String notice= editText.getText().toString();
Intent intentSaveNotice = new Intent(view.getContext(), YourNoticesActivity.class);
intentSaveNotice .putExtra("my notice", notice);
startActivity(intentSaveNotice );
}
});
}
}
so what i tried to do is that you first have nothing in the YourNoticesActivity
and after clicking on save you have 1 more notice and so on
i hope you could understand me

Runtime error when logging off android stuido mobile app

I have a java mobile app that is connected with Firebase, login and registration works perfect. I only get an error when I try to logout from the Home Page/Activity. This is the error i get and I don't know what to do.
Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference at com.example.platformapp.HomeActivity.onCreate(HomeActivity.java:37)
This is my code for the Home Activity/Page
public class HomeActivity extends AppCompatActivity {
Button btnLogout;
FirebaseAuth myFireBaseAuth;
TextView email;
FirebaseFirestore myfStore;
String userID;
private FirebaseAuth.AuthStateListener myAuthStateListener;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
email = findViewById(R.id.displayEmail);
myFireBaseAuth = FirebaseAuth.getInstance();
myfStore = FirebaseFirestore.getInstance();
userID = myFireBaseAuth.getCurrentUser().getUid(); // This is line 37
DocumentReference documentReference = myfStore.collection("users").document(userID);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@override
public void onEvent @nullable DocumentSnapshot documentSnapshot, @nullable FirebaseFirestoreException e) {
email.setText(documentSnapshot.getString("email"));
}
});
}
public void logout(View view)
{
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}

Categories

Resources