I have a listview in the SubActivity of my app.. but when I go from the subActivity to the main Activity and i try to return to the subActivity, the item of the listView disappear.. how to fix this problem?
also if I close my app and i restart it, my listview is empty... why?
my MainActivity:
final ArrayList<String> listCron = new ArrayList<String>();
....
#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();
switch(id) {
case R.id.history:
Intent history = new Intent(getApplicationContext(), HistoryClass.class);
history.putStringArrayListExtra("history", listCron);
startActivity(history);
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
my SubActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent i = getIntent();
ArrayList<String> cronologia = i.getStringArrayListExtra("history");
ListView lv = (ListView) findViewById(R.id.viewCron);
if (cronologia.isEmpty()) {
cronologia.add("No operations");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_layout, cronologia);
lv.setAdapter(adapter);
}
}
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HistoryClass"
android:label="#string/history">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>
Related
When I tried to give different titles in AndroidManifest.xml for each activity, app shows first title for all children activities instead off given titles. I'm new in coding so sorry for wrong explanation of my problem. The code is as following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.miwok">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NumbersActivity"
android:label="#string/category_numbers" />
<activity
android:name=".FamilyActivity"
android:label="#string/category_family" />
<activity
android:name=".ColorsActivity"
android:label="#string/category_colors" />
<activity
android:name=".PhrasesActivity"
android:label="#string/category_phrases" />
</application>
</manifest>
How can I solve it?
Update: to make it more clear I'm adding my MainActivity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
TextView numbers = (TextView) findViewById(R.id.numbers);
TextView colors = (TextView) findViewById(R.id.colors);
TextView family = (TextView) findViewById(R.id.family);
TextView phrases = (TextView) findViewById(R.id.phrases);
assert numbers != null;
numbers.setOnClickListener(this);
assert colors != null;
colors.setOnClickListener(this);
assert family != null;
family.setOnClickListener(this);
assert phrases != null;
phrases.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.numbers:
Intent numberIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(numberIntent);
break;
case R.id.colors:
Intent colorsIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(colorsIntent);
break;
case R.id.family:
Intent familyIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(familyIntent);
break;
case R.id.phrases:
Intent phrasesIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(phrasesIntent);
break;
}
}
}
Second update:
When I copied Intents for each activity, I forgot to change activity names, so problem was ot in title but in opened activity.
You can name it in code:
by
actionbar.setTitle("title here");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!MyApplication.getInstance().isTablet())
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_settings);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getString(R.string.settings));
....
}
So when I copied MainActivity.java from course example, all titles start working normally. The difference is that I used 'switch (view.getId())' instead of creating new onClick for each activity. I still didn't understand why it doesn't work, but hope I will learn it with more experience.
Summary:
Using 'switch' statement for activities can cause problems (for ones without knowledge,like me) in children activities titles.
New to android and I can't seem to be able to send any text to myself on my emulator. I have allowed permission in the manifest file and imported the necessary classes but still can't seem to figure out what's wrong. Any help would be greatly appreciated.
public class Detail extends AppCompatActivity {
public Button button5;
Spinner spinner2;
ArrayAdapter<CharSequence> adapter;
EditText editText4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
editText4 = (EditText) this.findViewById(R.id.editText4);
button5 = (Button) findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String contact = editText4.getText().toString();
String message = "Help is on the way.";
sendMessage(contact,message);
Intent b = new Intent(Detail.this, End.class);
startActivity(b);
}
});
spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter = ArrayAdapter.createFromResource(this,R.array.selectissue,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position) + " selected" , Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void sendMessage(String contact, String message)
{
SmsManager smsManager = SmsManager.getDefault();
try {
smsManager.sendTextMessage(contact, null , message , null , null);
Toast.makeText(getApplicationContext(), "SMS Sent.", Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), "SMS failed to send.", Toast.LENGTH_LONG).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.menu_detail, 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);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".Option"
android:label="#string/title_activity_option" >
</activity>
<activity
android:name=".Detail"
android:label="#string/title_activity_detail" >
</activity>
<activity
android:name=".End"
android:label="#string/title_activity_end" >
</activity>
<activity
android:name=".Feedback"
android:label="#string/title_activity_feedback" >
</activity>
<activity
android:name=".Student"
android:label="#string/title_activity_student" >
</activity>
<activity
android:name=".Incident"
android:label="#string/title_activity_incident" >
</activity>
<activity
android:name=".Help"
android:label="#string/title_activity_help" >
</activity>
<activity
android:name=".Wifi"
android:label="#string/title_activity_wifi" >
</activity>
<activity
android:name=".Basic"
android:label="#string/title_activity_basic" >
</activity>
<activity
android:name=".Home"
android:label="#string/title_activity_home" >
</activity>
<activity
android:name=".Guide"
android:label="#string/title_activity_guide" >
</activity>
<activity
android:name=".VPN"
android:label="#string/title_activity_vpn" >
</activity>
<activity
android:name=".VDI"
android:label="#string/title_activity_vdi" >
</activity>
<activity
android:name=".Location"
android:label="#string/title_activity_location" >
</activity>
<activity
android:name=".Phone"
android:label="#string/title_activity_phone" >
</activity>
<activity
android:name=".config"
android:label="#string/title_activity_config" >
</activity>
<activity
android:name=".Script"
android:label="#string/title_activity_script" >
</activity>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id" />
<activity
android:name="com.facebook.FacebookActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" />
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProvider"
android:exported="true" />
<activity
android:name=".Register"
android:label="#string/title_activity_register" >
</activity>
<activity
android:name=".Signup"
android:label="#string/title_activity_signup" >
</activity>
</application>
You can not send text messages via emulator if you want to test SMS feature then test it on real android device with activated sim card.
I coded this in order to get a Filter icon at OptionsMenu.I was using an empty meny with drawable image and the onClick of that was showing a DialogFragment. But this doesn't work fine as an empty title item appears on the OptionsMenu clicking which my DialogFragment gets open.
I appreciate any help.
Thanks.
menu file for OptionsMenu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/filter"
android:orderInCategory="100"
android:icon="#drawable/filter_icon"
android:showAsAction="ifRoom|withText"
android:title="hi" />
</menu>
This is myactivity :
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
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.
String[] values = new String[]{ "Veg.",
"Non Veg.",
"Veg & Non veg."
};
int id = item.getItemId();
if (id == R.id.filter) {
RestaurantListingFragment restaurant_categories_dialog = new RestaurantListingFragment();
android.app.FragmentManager fm = getFragmentManager();
Bundle args = new Bundle();
args.putStringArray("restaurantCategories",values);
restaurant_categories_dialog.setArguments(args);
// Show DialogFragment
restaurant_categories_dialog.show(fm, "hi");
return true;
}
return super.onOptionsItemSelected(item);
}
This is my fragment class:
public class RestaurantListingFragment extends DialogFragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dialog_restaurant_categories, container,false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
String[] restaurantCategories = getArguments().getStringArray("restaurantCategories");
if(restaurantCategories != null)
{
ListView lv=(ListView) rootView.findViewById(R.id.fd_lv);
ArrayAdapter arrayAdapter =new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,android.R.id.text1,restaurantCategories);
lv.setAdapter(arrayAdapter);
}
return rootView;
}
}
Manifests :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cresol.demo.drestodemo">
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:supportsRtl="true">
<activity
android:name=".Home"
android:theme="#style/NoTitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".signup"
android:theme="#style/NoTitle" />
<activity
android:name=".RestaurantListing"
android:theme="#style/AppTheme" />
<activity android:name=".DishListing"
android:theme="#style/AppTheme"></activity>
</application>
</manifest>
In your menu file change this line
android:showAsAction="ifRoom|withText"
with this one
app:showAsAction="always"
I've been at this for hours and can't figure it out. When I debug, it gives the above error. I'm new to this so go gentle if it's something obvious that I"m missing...
here's the code which introduced the crash from the first activity, it's still labeled 'Main':
//-- Menu Press --
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//-- Handle item selection
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent menu = new Intent(this, Menu.class);
menu.putExtra("plWin", plWin);
menu.putExtra("plLoss", plLoss);
menu.putExtra("plDraw", plDraw);
startActivity(menu);
return true;
case R.id.menu_reset:
if (opCounter > plCounter) {
plLoss++;
}else if (opCounter < plCounter) {
plWin++;
}else {
plDraw++;
}
opCounter = 0;
plCounter = 0;
return true;
default:
return false;
}
}
and the second activity, currently named 'Menu':
public class Menu extends Activity {
Intent menu = getIntent();
int wins = menu.getIntExtra("plWin", 0);
int losses = menu.getIntExtra("plLoss", 0);
int draws = menu.getIntExtra("plDraw", 0);
private TextView winNum = null;
private TextView lossNum = null;
private TextView drawNum = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//-- Win Counter --
LinearLayout winView = (LinearLayout) findViewById(R.id.plLeft);
winNum = (TextView) winView.findViewById(R.id.winNum);
winNum.setText("" + wins);
//-- loss Counter --
LinearLayout lossView = (LinearLayout) findViewById(R.id.plMid);
lossNum = (TextView) lossView.findViewById(R.id.lossNum);
lossNum.setText("" + losses);
//-- Draw Counter --
LinearLayout drawView = (LinearLayout) findViewById(R.id.plRight);
drawNum = (TextView) drawView.findViewById(R.id.drawNum);
drawNum.setText("" + draws);
}
public void onBackPressed() {
finish();
super.onBackPressed();
}
}
and here's my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sesto.life.counter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.sesto.life.counter.Menu"
android:label="#string/title_activity_menu" >
</activity>
</application>
I'm looking for ideas. Thank you all in advance
I only see one possible mistake is at Intent menu = new Intent(this, Menu.class);
. Please check Menu.class is your Menu class but not Android Menu View (located at android.view.Menu). The logcat told that it can not find android.view.Menu in manifest. Hover your mouse pointer above Menu.class to check which class you imported for this.
Change
<activity
android:name="com.sesto.life.counter.Menu"
android:label="#string/title_activity_menu" >
</activity>
to
<activity
android:name=".Menu"
android:label="#string/title_activity_menu" >
</activity>
and see if that helps
I got this error message when I forgot to add The Activity in Manifest file.
So i have this activity :
public class settings_dock extends Activity {
AlertDialog alert;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.settings);
//Weather settings
ListView listView = (ListView) findViewById(R.id.settings);
String[] values = new String[] { "Enable/Disable","Configure Item 1","Configure Item 2","Configure Item 3","Configure Item 4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
final CharSequence[] items = {"Enabled", "Disabled"};
AlertDialog.Builder builder = new AlertDialog.Builder(settings_dock.this);
builder.setTitle("The Dock is ....");
SharedPreferences dock = app.getContext().getSharedPreferences("dock",app.getContext().MODE_WORLD_READABLE);
builder.setSingleChoiceItems(items,dock.getInt("enabled_disabled",-1), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
SharedPreferences dock = app.getContext().getSharedPreferences("dock",app.getContext().MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = dock.edit();
prefsEditor.putInt("enabled_disabled",item);
prefsEditor.commit();
alert.dismiss();
if(item == 0)
{
}
if(item == 1)
{
}
}
});
alert = builder.create();
alert.show();
}
if(position == 1)
{
Intent in=new Intent(settings_dock.this,set_dock.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("dock_location",1);
}
if(position == 2)
{
Intent in=new Intent(settings_dock.this,set_dock.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("dock_location",2);
}
if(position == 3)
{
Intent in=new Intent(settings_dock.this,set_dock.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("dock_location",3);
}
if(position == 4)
{
Intent in=new Intent(settings_dock.this,set_dock.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("dock_location",4);
}
}
});
}
}
and I am trying to launch a intent as you can see
but simplay nothing happens not even a error in logcat
The intent to launch is lusted in my manifest
I am realy puzzeld right now :(
m
edit :
manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.alexander.fuchs.lockscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".app"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:excludeFromRecents="true"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="boot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
<service
android:name="lockservice"
android:process=":lockscreen"
android:icon="#drawable/ic_launcher"
android:label="Lockscreen">
</service>
<activity
android:name=".weather_update"
android:theme="#android:style/Theme.Translucent">
</activity>
<activity
android:name=".restart_service"
android:theme="#android:style/Theme.Translucent">
</activity>
<activity
android:name=".settings">
</activity>
<activity
android:name=".settings_weather">
</activity>
<activity
android:name=".settings_general">
</activity>
<activity
android:name=".settings_dock">
</activity>
<activity
android:name=".settings_personalisation">
</activity>
<activity
android:name=".set_dock"
android:theme="#android:style/Theme.Dialog"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
You haven't called:
startActivity(in);
Hopefully this helps.
Try adding startActivity(in) in each of the if statement. To start an activity you need to call startActivity(intent) method.