Crashing when testing - java

So, I've just started views and moving between activities. I thought I got what I was looking for. I fixed all my errors, but now when I test the application it crashes. I'm new to android and eclipse. So I'm not exactly sure what's happening. Here's my MainActivity.java:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String info = intent.getStringExtra("info_key");
TextView text = (TextView) findViewById(R.id.nametext);
text.setText(info);
}
//opens a new activity.
public void openAddItem (View v){
Intent intent = new Intent (this, Additem.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And here's my second activity; Additem.java:
package com.grocerylist;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Additem extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_additem);
}
public void additem (View v){
EditText text = (EditText)findViewById(R.id.itemname);
String info = text.getText().toString();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("info_key", info);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_additem, menu);
return true;
}
}
Thanks, and I'm positive the problem lies where I've placed my id's.

Your first problem I see is here
Intent intent = getIntent();
String info = intent.getStringExtra("info_key");
TextView text = (TextView) findViewById(R.id.nametext);
text.setText(info);
there is no Intent to "get" if this is your first Activity. That is used when you start an Activity with an Intent and send extras. So, naturally, info is null. There will be something there when you create it from your second Activity but not when you first run your app. Also, you may want to check into using startActivityForResult in your first Activity
Second, I don't see where you call openAddItem() in your first Activity. It may be from a Button but I don't see any Buttons
It looks like you are missing some key understandings of the fundamentals of the Android framework. I suggest you start with the Docs Here if you haven't been through them already. Good luck to you
Also, this
Intent intent = new Intent (this, Additem.class);
should be this
Intent intent = new Intent (MainActivity.this, Additem.class);

the first problem I also see was already mentioned by codeMagic.
The second thing: in case you just call your methods which start another Activity in onCreate(), you will get a pseudo-infinite number of those Activities in the stack and your app will crash.
Consider watching these tutorials, it may help you to get started

Related

Change button color when a button in another activity is pressed

I'm new in Android programming and I still have sometimes small problems. My problem currently is the following:
I have 2 activities with just one button in each. The button of the first activity is opening the second activity. But when I press the button in the second activity, the Text in the Button of the first activity should change to "Hello" and the color should be red.
I have managed to change the text but not the color. Could someone help me please?
My Code:
First activity:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button placeHolder;
Intent intent;
public void button0(View v){
intent = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
placeHolder = findViewById(R.id.button);
placeHolder.setText(getIntent().getStringExtra("message"));
}
}
The code of second activity:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends AppCompatActivity {
public void buttonOnClick(View v){
Intent intent=new Intent(MainActivity2.this, MainActivity.class);
intent.putExtra("message", "Hello");
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
Add this line of code in your first activity where you set your text to your button:
To change background color:
placeHolder.setBackgroundColor(Color.parseColor("#FF0000"));
To change text color:
placeHolder.setTextColor(Color.parseColor("#FF0000"));
Usage:
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button placeHolder;
Intent intent;
public static final int REQUEST_CODE = 101;
public void button0(View v){
intent = new Intent(getApplicationContext(), MainActivity2.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
placeHolder = findViewById(R.id.button);
}
// Call Back method to get the Message form other Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed
if(requestCode == REQUEST_CODE) {
placeHolder.setText(data.getStringExtra("message"));
placeHolder.setTextColor(Color.parseColor("#FF0000"));
}
}
MainActivity2:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends AppCompatActivity {
public void buttonOnClick(View v){
Intent intent=new Intent();
intent.putExtra("message", "Hello");
setResult(MainActivity2.REQUEST_CODE, intent);
finish();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
you can use startActivityforResult() insted of startActivity() and then when you come back from second class to the first class then set red color tor your button onActivityResult() function thanks
You can use GreenRobot. Then you can do anything from other activities.
Here is the link Event Bus
Second Activity:
Intent intent=new Intent(MainActivity2.this, MainActivity.class);
intent.putExtra("message", "Hello");
intent.putExtra("color", -65536); //-65536 is color RED int value
startActivity(intent);
First Activity
placeHolder.setText(getIntent().getStringExtra("message"));
placeHolder.setBackgroundColor(getIntent().getStringExtra("color",0)); // 0 is default value

Save markers to csv file

I am wondering how to when the user closes the app, save the markers to a file then load them back in when the app is opened again
Here is my code:
package com.example.mapapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends Activity
{
MapView mv;
StringBuffer filePath;
File file;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// This line sets the user agent, a requirement to download OSM maps
Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
setContentView(R.layout.activity_main);
mv = (MapView)findViewById(R.id.map1);
ItemizedIconOverlay<OverlayItem> items;
mv.setBuiltInZoomControls(true);
mv.getController().setZoom(11);
mv.getController().setCenter(new GeoPoint(53.3710,-1.4502));
}
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == R.id.addpoi)
{
// react to the menu item being selected...
Intent intent = new Intent(this,AddPoi.class);
startActivityForResult(intent,0);
return true;
}
Intent intent = new Intent(this,AddPoi.class);
startActivityForResult(intent,0);
return false;
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu_mad_assignment, menu);
return true;
}
protected void onActivityResult(int requestCode,int resultCode,Intent intent)
{
if(requestCode==0)
{
if (resultCode==RESULT_OK)
{
Bundle extras=intent.getExtras();
String poiName= extras.getString("poiName");
String poiType= extras.getString("poiType");
String poiDesc= extras.getString("poiDesc");
ItemizedIconOverlay<OverlayItem> items = new ItemizedIconOverlay<>(this, new ArrayList<OverlayItem>(), null);
OverlayItem marker = new OverlayItem(poiName, poiType,poiDesc, new GeoPoint(53.3710,-1.4502));
items.addItem(marker);
mv.getOverlays().add(items);
Log.d("NAME",poiName);
Log.d("NAME",poiType);
Log.d("NAME",poiDesc);
}
}
}
}
Add marker code:
package com.example.mapapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import com.example.luke.madassignment.R;
public class AddPoi extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_poi);
Button addPoiButton = (Button)findViewById(R.id.addPoi);
Button cancelButton = (Button)findViewById(R.id.cancel);
addPoiButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle=new Bundle();
EditText poiName = (EditText)findViewById(R.id.editTextPoiName);
EditText poiType = (EditText)findViewById(R.id.editTextPoiType);
EditText poiDesc = (EditText)findViewById(R.id.editTextPoiDesc);
String poiNameString = poiName.toString();
String poiTypeString = poiName.toString();
String poiDescString = poiName.toString();
bundle.putString("poiName",poiNameString);
bundle.putString("poiType",poiTypeString);
bundle.putString("poiDesc",poiDescString);
intent.putExtras(bundle);
setResult(RESULT_OK,intent);
finish();
}
}
The problem is i cant figure out a way to save the users marker to a file if the app is closed and then load back in the markers when the app is opened again
Use built in sharedPref, simple key value storage, to save data in persistence mode. Check doc here
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive data
types. You can use SharedPreferences to save any primitive data:
booleans, floats, ints, longs, and strings. This data will persist
across user sessions (even if your application is killed).
https://developer.android.com/guide/topics/data/data-storage.html#pref
Using OSMBonusPack, you can grab your markers in a KML structure, then save the KML file locally (in onStop).
And load back this KML file when the app is opened (in onCreate).

Linking two java activty's with button bug! "Android Studio"

Hi I'm quite new to Java could you please show where i've gone wrong would be much appreciated many thanks! I want to Link two java activty's with button when presed
package com.example.matt.androidui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends Activity {
Called at the top of activty.
Code i'm having trouble with seems to be something up!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener) {
public void onClick (View v){
Intent intent = new Intent (v.getContext(), MainActivity2Activity.class);
startActivityForResult(kntent, 0);
}
}
}
Button Code ends here!
#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) {
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
If you want to start second activity by pressing button then simply do this,
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);
#MattTheCodeMan
If you want to start a second activity by pressing a button then write this code:
Intent intent = new Intent (v.getContext(), MainActivity2Activity.class);
startActivityForResult(intent, 0);
If you are new in Android maybe is good start with the navigation edition in android studio, LOOK THIS LINK

Onclick imagebutton-action (menu)

I'm making an app and when it launches it starts Mainactivity.java
Mainactivity.java opens a layout with 9 Imagebuttons.
How can I implement in my code in Mainactivity.java that once one is clicked it opens another activity (like telefoonnummers.java)?
Sorry for my bad English but I'm dutch and a non-native speaker.
I have this code in Mainactivity.java:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
#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.activity_main, menu);
return true;
}
}
Very clean as you see, but how can I add the implementation, would you guys please help???
My Imagebuttons are all just called imagebutton1, imagebuttton2 etc. btw.
After
setContentView(R.layout.activity_main);
add for each ImageButton:
findViewById(R.id.imagebutton1).setOnClickListener(this);
Make class implement OnClickListener
class MainActivity extends Activity implements View.OnClickListener {
and add this method:
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.R.id.imagebutton1:
startActivity(new Intent(telefoonnummers.class));
break;
case R.id.R.id.imagebutton2:
startActivity(new Intent(telefoonnummers.class));
break;
//-- more cases --
}
}

No enclosing instance of the type DonationsActivity is accessible

I'm extremely new to all of this coding stuff, and I've gotten pretty far on my own, but I can't seem to figure out this error. All help is appreciated.
"No enclosing instance of the type DonationsActivity is accessible in scope"
package com.ganttbros.shadowui;
import org.donations.DonationsActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class DonateActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donate);
final Button donate = (Button) findViewById(R.id.donatebutton);
donate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent(DonationsActivity.this, DonationsActivity.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.donate, menu);
return true;
}
}
I should tell you that I am attempting to implement this library: https://github.com/dschuermann/android-donations-lib#readme
I have set up the button, but I just need to get it to launch the "DonationsActivity" when pressed.
use
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));
OR
startActivity(new Intent(v.getContext(), DonationsActivity.class));
instead of
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
for starting DonationsActivity Activity from DonateActivity Activity
Change this:
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
to:
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));

Categories