Second Activity doesent open up after clicking the button - java

I've been searching about this problem a lot before posting this question but still couldnt get any help,kindly help me in solving this problem in which when I press the button a black page appears and then the app closes saying its not responding,im a newbie over here and any kind of help will be appreciated.
activity_main
<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.firstapp.MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:ems="10"
android:inputType="numberPassword" />
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText1"
android:layout_alignLeft="#+id/editText1"
android:ems="10"
android:inputType="textMultiLine"
android:text="Enter your Password"
tools:ignore="HardcodedText" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText5"
android:layout_alignLeft="#+id/editText5"
android:layout_marginBottom="20dp"
android:ems="10"
android:inputType="textEmailAddress" >
<requestFocus android:layout_width="wrap_content" />
</EditText>
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText3"
android:layout_alignLeft="#+id/editText3"
android:ems="10"
android:inputType="textMultiLine"
android:text="Enter your E-mail id"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText4"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"
android:src="#drawable/robo"
android:contentDescription="#null"/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:ems="10"
android:inputType="textMultiLine"
android:text="Welcome To Foodparkk"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Login" />
</RelativeLayout>
MainActivity.java
package com.example.firstapp;
import com.example.firstapp.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),Second.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.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);
}
}
Second.java
package com.example.firstapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Second extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, 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);
}
}
Activity_Second.xml
<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.firstapp.Second" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
manifest code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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/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=".Second"
android:label="#string/title_activity_second" >
</activity>
</application>
</manifest>

public class MainActivity extends ActionBarActivity { //mainactivity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
then your manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Base.AppCompat.Light.DarkActionBar" > // i changed this
<activity
android:name=".MainActivity"
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=".Second"
android:label="#string/title_activity_second" >
</activity>
</application>
if you extend actionbarActivity its xml should use its style's, i dont wona story this, so i will repharse it in my edit. simply to say your second activity doesn't have style set so it uses the device default theme for the activity set in your application so copy paste the respective codes in mine to yours

It's called ANR short for Android Not Responding. Your app is doing so much processing in the main thread. Consider using a background Thread or AsyncTask along with ProgressDialog so that the main thread is not affected by the processing part.
Read more about it in the documentation. Also refer to this post for more info.

Related

Android Studio emulator white screen upon opening of app

Whenever I install and run my app in the Android Studio emulator all I get is a white screen and the tool bar above it even though in the preview it works completely fine. Heres the xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#drawable/rectanglestemlogo"
android:orientation="horizontal">
<Button
android:id="#+id/Home_Button"
android:layout_width="0dp"
android:layout_weight="0.33"
android:layout_height="wrap_content"
android:onClick="goToStemAcademy"
android:text="#string/Home_Button"
android:textAllCaps="false"
android:textColor="#000000" />
<Button
android:id="#+id/StemAcademy_Button"
android:layout_width="0dp"
android:layout_weight="0.33"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:text="#string/Stem_Academy"
android:textAllCaps="false"
android:textColor="#000000"
android:visibility="visible" />
<Button
android:id="#+id/Competitions_Button"
android:layout_width="0dp"
android:layout_weight="0.33"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:text="#string/Competitions"
android:textAllCaps="false"
android:textColor="#000000" />
</LinearLayout>
then the java:
package com.example.dmssteamapp6;
import android.content.Intent;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public void goToStemAcademy(View v) {
startActivity(new Intent(MainActivity.this, StemAcademy.class));
}
}
then the new xml activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="138dp"
android:onClick="goToHome"
android:text="Button" />
</LinearLayout>
and the java that goes with it:
package com.example.dmssteamapp6;
import android.content.Intent;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class StemAcademy extends AppCompatActivity {
public void goToHome(View v) {
startActivity(new Intent(StemAcademy.this, MainActivity.class));
}
}
also here is the androidmanifest.xml code if applicable:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dmssteamapp6">
<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=".MainActivity"
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>
<activity
android:name=".StemAcademy"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>
Just Create onCreate method in every java class
public class StemAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stemacademy);
}
public void goToHome(View v) {
startActivity(new Intent(StemAcademy.this, MainActivity.class));
}
}
Activity main class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goToStemAcademy(View v) {
startActivity(new Intent(MainActivity.this, StemAcademy.class));
}
}

Window already focused, ignoring focus gain of: com.android.internal.view in my notification app

i have developed a notification application where on clicking on nitification one activity need to open . but when i click on the notification icon at the top corner of my emulator nothing happens. it should open the NotificationView activity. in the logcat data i found a warning W/InputMethodManagerService(1214): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#b4e4dbd8 attribute=null, token = android.os.BinderProxy#b4a95d50
files are as bellow ...
MainActivity.java
package com.example.notification1;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
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 {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Notify("You've received new message","ghfghfghfghf hfghfdgh f fgj f gjdf fghfg");
}
});
}
private void Notify(String notificationTitle, String notificationMessage){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
#SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.ic_launcher,"New Message", System.currentTimeMillis());
Intent notificationIntent = new Intent(this,NotificationView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle,notificationMessage, pendingIntent);
int notificationId = (int) (System.currentTimeMillis()/1000);
notificationManager.notify(notificationId, notification);
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
NotificationView.java
package com.example.notification1;
import android.os.Bundle;
import android.app.Activity;
public class NotificationView extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notification1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<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=".NotificationView"
android:label="#string/app_name" />
</application>
</manifest>
activity_main.xml
<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="MainActivity">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification Example"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#drawable/ic_launcher"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:id="#+id/button"
android:layout_marginTop="62dp"
android:layout_below="#+id/imageButton"
android:layout_centerHorizontal="true" />
</RelativeLayout>
notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:text="Hi, Your Detailed notification view goes here...." />
</LinearLayout>
Try adding FLAG_ACTIVITY_REORDER_TO_FRONT, or the combination of FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP, to the Intent
Also add a task stach builder
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
hope,this will help you

Android Studio error after splash

I have followed and tutorial for 4 times to make a splash screen but after the splash screen the app gives an error and closes. The splash screen has an image which stays for 4 second and the next screen has an webviewer, above the viewer the title bar needs to be there .Any idea?
Android Honey Comb 3.0 is set as default
The Splash.java :
package nl.broodje.app.broodje;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Christiaan on 14-11-2015.
*/
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread myThread = new Thread(){
#Override
public void run() {
try {
sleep(3000);
Intent startMainScreen = new Intent(getApplicationContext(), MainActivity.class);
startActivity(startMainScreen);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
The splash_screen.xml :
<?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">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/logo"
android:id="#+id/imageView"
android:layout_gravity="center_vertical" />
</LinearLayout>
The content_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
The activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
The MainActivity.java :
package nl.broodjep.app.broodje;
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.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://broodje.nl/";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings() .setJavaScriptEnabled(true);
view.loadUrl(url);
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();
}
});
}
#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);
}
}
The AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.broodje.app.broodje" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/icoon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
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>
<activity android:name=".MainActivity"/>
</application>
</manifest>
Since MainActivity extends AppCompatActivity, and you're setting your own ActionBar, MainActivity needs to have a NoActionBar theme, like the splash screen.
<activity android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar" />

When I run my app on Android Studio, emulator starts but it says that the test app crashed

I am trying to create a sign in sign up page. I have 3 pages ( activities). All I do is click a sign up or sign in button on main activity and it takes me to other pages.
Here is the Java code:
package com.appt.shreyabisht.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button btn_sign;
Button btn_sign_up;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_sign = (Button) findViewById(R.id.btn_sign);
btn_sign.setOnClickListener(this);
btn_sign_up = (Button) findViewById(R.id.btn_sign_up);
btn_sign_up.setOnClickListener(this);
}
#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);
}
private void btclick() {
startActivity(new Intent("com.appt.shreyabisht.second"));
}
private void btsignclick() {
startActivity(new Intent("com.appt.shreyabisht.third"));
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_sign:
btclick();
break;
case R.id.btn_sign_up:
btsignclick();
break;
}
}
}
I am not sure how multiple buttons work and if I should create a separate public void onClick(View view) for another button. Please suggest.
When I run the app, the error I get is :
FATAL EXCEPTION: main
Process: com.appt.shreyabisht.test, PID: 832
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appt.shreyabisht.test/com.appt.shreyabisht.test.MainActivity}
Caused by:java.lang.NullPointerException
at com.appt.shreyabisht.test.MainActivity.onCreate(MainActivity.java:25)
(There are more lines but I think this is the one causing the crash)
Here is the activity_main.xml:
<LinearLayout 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:padding="#dimen/activity_horizontal_margin"
android:gravity="center|bottom"
tools:context=".MainActivity"
android:background="#drawable/backw"
android:weightSum="1">
<Button
android:layout_width="89dp"
android:layout_height="wrap_content"
android:text="Sign in"
android:textColor="#ffffff"
android:id="#+id/btn_sign"
android:textStyle="bold"
android:layout_gravity="bottom"
android:onClick="buttonOnClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:gravity="center|center_vertical"
android:textStyle="italic"
android:text="#string/App_name"
android:id="#+id/textView"
android:layout_gravity="center_vertical"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sign_up_btn"
android:id="#+id/sign_up"
android:textColor="#ffffff"
android:layout_gravity="bottom" />
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.appt.shreyabisht.test.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="com.appt.shreyabisht.test.second"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.appt.shreyabisht.second" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.appt.shreyabisht.test.third"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.appt.shreyabisht.third" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
the problem is here :
btn_sign_up = (Button) findViewById(R.id.btn_sign_up);
you are trying to find a view by id = btn_sign_up but you are defining it by another id here :
android:id="#+id/sign_up"

no resource found that matches the given name at title

R is not generating in my android application package, and I have errors on all of my java files without changing anything on these files , I manipulated these files :
strings :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Washing Machines System</string>
<string name="action_settings">something here</string>
<string name="details">insert your details</string>
<string name="sign">Sign in</string>
<string name="title_activity_rooms">Rooms</string>
<string name="hello_world">Hello world!</string>
</resources>
mainfeast :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.washingsystem"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/download"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<activity
android:name="com.example.washingsystem.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="com.example.washingsystem.Rooms"
android:label="#string/title_activity_rooms" >
</activity>
</application>
</manifest>
MainActivity:
package com.example.washingsystem;
import android.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button i;
#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;
}
public void signin(View view)
{
//Toast.makeText(this,"AAAAAAAAAA",Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, Rooms.class);
MainActivity.this.startActivity(myIntent);
}
#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;
}
}
}
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:visibility="visible"
tools:ignore="MergeRootFrame" >
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="17dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="136dp"
android:onClick="signin"
android:text="#string/sign" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignLeft="#+id/editText2"
android:layout_marginBottom="62dp"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="#+id/textView1"
android:layout_width="212dp"
android:layout_height="52dp"
android:layout_above="#+id/editText2"
android:layout_alignRight="#+id/editText2"
android:layout_marginBottom="25dp"
android:text="#string/details"
android:textColor="#color/Black"
android:textSize="20sp" />
</RelativeLayout>
R not generating because you have import android.R;
So remove the import android.R;
and make sure there will be no errors in your xml files and clean and build the project again

Categories

Resources