App not displaying anything when run - java

I am only a day old in using android studio. I have to create an app for my school project, i did that, i programmed it in android studio and it had no errors. Please help,
this is the Main activity named frontpagee
package com.example.lenovo.shop_easy;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class frontpagee extends Activity
{
public void sendMessage(View view){
Intent intent = new Intent(frontpagee.this, catalog.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.menu_frontpagee, 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);
}
}
the XML goes like this
i want to move from this activity to the second ACTIVITY which is named Catalog
java is
package com.example.lenovo.shop_easy;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class catalog extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
}
#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_catalog, 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);
}
public void sendMessage(View view) {
Intent intent = new Intent(catalog.this, product1.class);
startActivity(intent);
}
public void sendMessage1(View view) {
Intent intent = new Intent(catalog.this, PRODUCT2.class);
startActivity(intent);
}
public void sendMessage2(View view) {
Intent intent = new Intent(catalog.this, PRODUCT3.class);
startActivity(intent);
}
public void sendMessage3(View view) {
Intent intent = new Intent(catalog.this, product4.class);
startActivity(intent);
}
public void sendMessage4(View view) {
Intent intent = new Intent(catalog.this, product5.class);
startActivity(intent);
}
public void sendMessage5(View view) {
Intent intent = new Intent(catalog.this, product6.class);
startActivity(intent);
}
public void sendMessage6(View view) {
Intent intent = new Intent(catalog.this, product7.class);
startActivity(intent);
}
public void sendMessage7(View view) {
Intent intent = new Intent(catalog.this, product8.class);
startActivity(intent);
}
public void sendMessage8(View view) {
Intent intent = new Intent(catalog.this, product9.class);
startActivity(intent);
}
public void sendMessage9(View view) {
Intent intent = new Intent(catalog.this, product10.class);
startActivity(intent);
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".frontpagee"
android:background="#drawable/back">
<ImageView
android:layout_width="472dp"
android:layout_height="500dp"
android:id="#+id/imageView11"
android:src="#mipmap/cover"
android:layout_gravity="center_horizontal|top"
android:contentDescription="#string/content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text12"
android:id="#+id/button11"
android:layout_gravity="center_horizontal|bottom"
android:textSize="#dimen/size2"
android:clickable="true"
android:onClick="sendMessage"/>
</FrameLayout>
the code is correct says android studio but when i run the adb on my phone it installs the app, everything goes fine but when i open the app it displays nothing but only the app icon and name at the top and nothing else what to do. please help
and here the main xml file also
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.shop_easy" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".frontpagee"
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=".catalog"
android:label="#string/title_activity_catalog" >
</activity>
<activity
android:name=".product1"
android:label="#string/title_activity_product1" >
</activity>
<activity
android:name=".PRODUCT2"
android:label="#string/title_activity_product2" >
</activity>
<activity
android:name=".PRODUCT3"
android:label="#string/title_activity_product3" >
</activity>
<activity
android:name=".product4"
android:label="#string/title_activity_product4" >
</activity>
<activity
android:name=".product5"
android:label="#string/title_activity_product5" >
</activity>
<activity
android:name=".product6"
android:label="#string/title_activity_product6" >
</activity>
<activity
android:name=".product7"
android:label="#string/title_activity_product7" >
</activity>
<activity
android:name=".product8"
android:label="#string/title_activity_product8" >
</activity>
<activity
android:name=".product9"
android:label="#string/title_activity_product9" >
</activity>
<activity
android:name=".product10"
android:label="#string/title_activity_product10" >
</activity>
</application>
</manifest>
what to do

You have not overridden the onCreate(Bundle savedInstance) method in class frontpagee and have not called setContentView(R.layout...) in it.

Related

Android BroadcastReceiver in ScrollActivity

I had working BroadcastReceiver which notifys user about network status changes, but since i added ScrollActivity to my app, the receiver is not working anymore. So i tried to make a blank ScrollActivity template project to test the reciever and i can see the same - the receiver wont work in apps with ScrollActivity. im a little bit desperate with this situation, would be thankful for help.
I have permissions and receiver registred in manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_app">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".ScrollingActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ConnectivityStatusReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
Here comes the receiver code, which hasnt been called even once in scrollactivity apps yet..
package com.example.test_app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class ConnectivityStatusReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//THIS HASNT BEEN CALLED EVEN ONCE YET SINCE SCROLLACTIVITY
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
Log.d("RECIEVER_LOG", "intent:"+intent.getAction());
if(activeNetworkInfo.isConnected()){
Log.d("NETWORK_LOG", "connected.");
}else{
Log.d("NETWORK_LOG", "not connected.");
}
if (activeNetworkInfo == null){
//no connection activity opens on connection loss
context.startActivity(new Intent(context , NoInternetConnection.class));
}
}
}
And here is the Scrolling activity itself, untouched as Android Studio generated it
package com.example.test_app;
import android.os.Bundle;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class ScrollingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CollapsingToolbarLayout toolBarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
toolBarLayout.setTitle(getTitle());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).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_scrolling, 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);
}
}
Would be thankful for any help.

Android Studio: SearchView: error in searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));

I'm a newbie working on an android app, following is the first screen of the app. I want my application to perform search using that search dialog box.
For this, I followed http://developer.android.com/guide/topics/search/search-dialog.html
But I was getting following error:
Logcat:
FATAL EXCEPTION: main
Process: com.example.shalini.circlelayout, PID: 19330
java.lang.NullPointerException
at com.example.shalini.circlelayout.MainActivity.onCreateOptionsMenu(MainActivity.java:69)
where line:69 in MainActivity.java is:
ComponentName cn = new ComponentName(this, SearchResultsActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
I referred to this
Cannot get searchview in actionbar to work
and made changes but the same problem still persists. My question is why is it showing error in searchView.setSearchableInfo(searchManager.getSearchableInfo(cn)); line. I couldn't figure that out, I still think that error is arising due to some other part of the code, maybe searchresultsactivity.xml or SearchResultsActivity.java. Any help is much appreciated. Thank you.
Code below:
MainActivity.java
package com.example.shalini.circlelayout;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
EditText e1;
ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.search);
addListenerOnButton();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
private void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), SymptomsList.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_main, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
ComponentName cn = new ComponentName(this, SearchResultsActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
//call to getSearchableInfo()
//SearchableInfo object
//SearchView starts an activity with the ACTION_SEARCH intent when a user submits a query
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);
}
}
SearchResultsActivity.java
package com.example.shalini.circlelayout;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SearchResultsActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
#Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
//use the query to search your data somehow
}
}
private void doMySearch(String query) {
Log.d("Event", query);
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="wrap_content" android:padding="#dimen/activity_horizontal_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity"
android:background="#drawable/blue_bluecrop1">
<EditText
android:layout_width="160dp"
android:layout_height="60dp"
android:id="#+id/search"
android:inputType="text"
android:hint="#string/sym"
android:drawableStart="#drawable/search_s2"
android:drawableLeft="#drawable/search_s2"
android:padding="20dp"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:background="#drawable/roundededittext"/>
<ImageButton
android:layout_width="40dp"
android:layout_height="45dp"
android:contentDescription="#string/symbutton"
android:id="#+id/imageButton"
android:background="#00000000"
android:layout_marginLeft="160dp"
android:layout_marginStart="160dp"
android:layout_marginTop="270dp"
android:clickable="true"
/>
searchresultsactivity.xml
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
#xml/searchable
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_searchable">
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shalini.circlelayout" >
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/doraemon" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
<activity android:name=".SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
</application>
Your searchView seems to be null. How did you make it?

Android - Go to new screen on button click

Followed a tutorial but doesnt seem to work on emulation.
MainActivity.java:
package b.myfirstapp;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#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.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);
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
}
}
Activity2.java:
package b.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Activity2 extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="b.myfirstapp" >
<application
android:allowBackup="true"
android:icon="#drawable/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:label="#string/app_name"
android:name=".Activity2" >
</activity>
</application>
</manifest>
There are no errors in the code, its just that when i emulate it it doesnt work, maybe ive coded it incorrectly, not too sure, it looks fine to me.
I'm new to android dev so im confused. Help!
UPDATED: (For clearance purposes with comments properly inserted)
I think you are missing the call to addListenerOnButton in your MainActivity class, for example inside onCreate right after setContentView....
Also, you should make it private since it's only going to be called from within this class.
You have not called your addListenerOnButton method so you have not set onClick handler to your button.
Your onCreate should look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton()
}

Failed to receive Intent from parent activity?

I'm following the tutorial from the following link: https://developer.android.com/training/basics/firstapp/starting-activity.html
The problem I'm facing is, I able to trigger the sendMessage method however it just failed to show the expected result?
Please take a look at the following code:
activity_main.xml (Parent layout):
<EditText android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
MainActivity.java:
package com.example.myfirstapp;
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.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#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);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
}
activity_display_message.xml (Children layout):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
DisplayMessageActivity.java:
package com.example.myfirstapp;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_display_message,
container, false);
return rootView;
}
}
}
MyFirstApp Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/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=".DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
P/S: I've set a breakpoint at DisplayMessageActivity.java onCreate() method but it wasn't being triggered.
You are missing a startActivity statement. Your MainActivity.sendMessage it should be like the follwing:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent); /// <---- THIS LINE
}
Hope it helps
Add below line
startActivity(intent);
under
intent.putExtra(EXTRA_MESSAGE, message);
And Call sendMessage() on button click
Add
startActivity(intent)
after this line
intent.putExtra(EXTRA_MESSAGE, message);
This why there's no response upon clicking the button.
1) You are not attaching Listener to your button. Do it like :
Button myButton = (Button) findViewById(R.id.my_button);
myButton.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
sendMessage(); //this is the action to be taken.
}
});
2) You are creating the Intent object, but not firing it. Use startActivity(intent) as the last line of sendMessage() method to fire the intent and get the desired results.

Shifting between activities (Android)

Shifting between activities isn't working.
What the app does is it opens the android.xml file then after 5 secs open the main file
Here's the code:
android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.android.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.android"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ANDROID" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android.java file:
package com.example.android;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class android extends Activity{
MediaPlayer sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.android);
sound1 = MediaPlayer.create(android.this , R.raw.android);
sound1.start();
Thread timer = new Thread()
{public void run()
{
try{
sleep(5000);
}
catch(InterruptedException ac){
ac.printStackTrace();
}
finally{
Intent openactivity = new Intent ("android.intent.action.MAIN");
startActivity(openactivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sound1.release();
finish();
}
}
Mainactivity.java:
package com.example.android;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
android.xml file:(the first file that launches up)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background = "#drawable/android"
>
</LinearLayout>
Try this way
Intent openactivity = new Intent(android.this,MainActivity.class);
startActivity(openactivity);
And for more information go to starting-new-activity
First, always Capitalise 1st letter of class names (code convention),
and then write your intents like below:
Intent intent = new Intent(android.this, MainActivity.class);
startActivity(intent );
You can check Starting Another Activity in Android Developers website.

Categories

Resources