I have a problem in this app, it show me "Unfortunately, app has stopped"
MainActivity.class
package ahmedchtn.stockmanager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText username;
EditText password;
Button loginbutton;
Button bpasschange;
int counter = 3;
String username_string ;
String password1 = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.identifer);
password = (EditText) findViewById(R.id.password);
loginbutton = (Button) findViewById(R.id.bLogin);
bpasschange = (Button) findViewById(R.id.bCh);
username_string = "admin";
if (password1 == "") {
password1 = "admin";
} else {
Intent intent4 = getIntent();
password1 = intent4.getStringExtra("oldpassw");
}
bpasschange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intentoldpass = new Intent(MainActivity.this, PasswordChange.class);
intentoldpass.putExtra("oldpassw", password1);
Intent intentpasschan = new Intent(getApplicationContext(), PasswordChange.class);
startActivity(intentpasschan);
}
});
loginbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (username.getText().toString().equals(username_string) &&
password.getText().toString().equals(password1)) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
Intent iop = new Intent(getApplicationContext(), ManagementPage.class);
startActivity(iop);
} else {
Toast.makeText(getApplicationContext(),
"Wrong Entries", Toast.LENGTH_SHORT).show();
counter--;
if (counter == 0) {
loginbutton.setEnabled(false);
}
}
}
}
);
}
}
PasswordChange.class
package ahmedchtn.stockmanager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PasswordChange extends AppCompatActivity {
Button bchange;
EditText oldpassword;
EditText newpassword;
String oldpass;
String newpass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password_change);
bchange = (Button) findViewById(R.id.bChange2);
oldpassword = (EditText) findViewById(R.id.old_password);
newpassword = (EditText) findViewById(R.id.new_password);
bchange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent ioldpass = getIntent();
oldpass = ioldpass.getStringExtra("oldpassw");
if (oldpass == (oldpassword.getText().toString())) {
newpass = newpassword.getText().toString();
Intent intentnew = new Intent(PasswordChange.this, MainActivity.class);
intentnew.putExtra("passnew", newpass);
startActivity(intentnew);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
});
}
}
I think that the problem is with the intents..
Thanks in advance for your help !
Is there any other solutions
The logcat
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at ahmedchtn.stockmanager.MainActivity$2.onClick(MainActivity.java:46)
at android.view.View.performClick(View.java:4439)
at android.widget.Button.performClick(Button.java:139)
at android.view.View$PerformClick.run(View.java:18395)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.
Xml Codes
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="ahmedchtn.stockmanager.MainActivity">
<EditText
android:id="#+id/identifer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_id" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/bLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/login" />
<Button
android:id="#+id/bCh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="changer passe" />
</LinearLayout>
</LinearLayout>
activity_password_change.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_password_change"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="ahmedchtn.stockmanager.PasswordChange">
<EditText
android:id="#+id/old_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_old_password" />
<EditText
android:id="#+id/new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_new_password" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/activity_vertical_margin"
android:weightSum="1">
<Button
android:id="#+id/bChange2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="0.5"
android:text="#string/confirm" />
<Button
android:id="#+id/main_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="0.5"
android:text="#string/main_page" />
</LinearLayout>
</LinearLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ahmedchtn.stockmanager">
<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=".ManagementPage"
android:parentActivityName=".MainActivity" />
<activity android:name=".ProductsSearch" />
<activity android:name=".ProductsCommand" />
<activity
android:name=".Editor"
android:parentActivityName=".ProductsList" />
<activity android:name=".ProductsList" />
<provider
android:name=".data.ProductProvider"
android:authorities="ahmedchtn.stockmanager"
android:exported="false" />
<activity android:name=".PasswordChange"></activity>
</application>
</manifest>
I have updated my code
From the information you've provided so far (NPE on line 28 of PasswordChange.class), it looks like this line is causing the issue:
bchange = (Button) findViewById(R.id.bChange);
bchange.setOnClickListener(new View.OnClickListener() {
...
You have a button named bChange in both of your layout xml files - change one of them into something unique. I highly recommend that all components should be uniquely named through the app, as that stops the possibility of an activity accidentally referencing the wrong resource.
Example:
activity_password_change.xml
...
<Button
android:id="#+id/bPasswordChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="0.5"
android:text="#string/confirm" />
...
PasswordChange.java
...
bchange = (Button) findViewById(R.id.bPasswordChange);
...
For the second issue that's arisen in MainActivity.java:
Intent intentbool = getIntent();
bmain = intentbool.getBooleanExtra("bool", bmain);
However, your problem here lies with line 20:
Boolean bmain;
Because you've declared a Boolean object rather than a boolean primitive (note the capital B), it is null by default as you've not initialised it. Change it to a primitive:
boolean bmain;
Make sure you have defined PasswordChange.class in your Manifest.xml file.
An example would be <activity android:name=".PasswordChange"/>. That's the most common reason for an app to crash when going to a new activity.
Related
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
findViewById() returns null when I call it in onCreate()
(3 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
I am trying to open up the second activity (MainActivity2) via a button but the application crashes every time, I am not quite sure how to solve this. I want to be able to open to the second screen to use the second activity. Thank you for your help. (Having trouble with separating the code, so I have used lines)
Activity main xml + Main activity
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="241dp"
android:text="Activity1"
android:id="#+id/textView"/>
<Button
android:id="#+id/button"
android:layout_width="375dp"
android:layout_height="101dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="275dp"
android:text="open password generator" />
</RelativeLayout>
**Main activity**
package com.example.menutest;
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 {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
activity main2 + activity main 2 XML + manifest
<?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"
tools:context=".MainActivity2">
<TextView
android:id="#+id/textView3"
android:layout_width="203dp"
android:layout_height="85dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="67dp"
android:layout_marginLeft="67dp"
android:layout_marginTop="264dp"
android:hapticFeedbackEnabled="false"
android:textColor="#070505"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.653"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="19dp"
android:layout_marginLeft="19dp"
android:layout_marginBottom="304dp"
android:hapticFeedbackEnabled="true"
android:soundEffectsEnabled="false"
android:text="Generate strong password"
android:textSize="18sp"
android:textStyle="bold"
app:backgroundTint="#36A13A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.59" />
</RelativeLayout>
***Mainactivity2***
package com.example.menutest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity2 extends AppCompatActivity {
Button button;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(generateString(16));
}
});
}
private String generateString(int length) {
char[] chars ="/.,MNBVCXZ;LKJHGFDSAPOIUYTREWQ[]#=-)0987654321!£$*&qwertyuiopzxcvbnmasdfghjkl(".toCharArray();
StringBuilder stringBuilder = new StringBuilder();
Random random = new Random();
for ( int i=0; i<length;i++)
{
char c = chars[random.nextInt(chars.length)];
stringBuilder.append(c);
}
return stringBuilder.toString();
}
}
***AndroidManifest xml***
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.menutest">
<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/Theme.MENUTEST">
<activity android:name=".MainActivity2"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
..........................................................................
Mainactivity2
button = (Button)findViewById(R.id.button4);
textView = (TextView)findViewById(R.id.textView3);
I'm a couple days into learning Android development and I'll admit my code is not pretty, as I have a few weeks of Java under my belt on top of this. I'm trying to create a back button on my 2nd main activity page to get back to the 1st main activity, but the only solutions I've come across are while using the toolbar. How would you go about adding this function while using noActionBar?
Main Activity 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"
android:background="#drawable/clearwater"
android:scaleType="centerCrop"
tools:context="com.example.goodvibes.helloworld.MainActivity">
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="146dp"
android:background="#drawable/btn"
android:fontFamily="cursive"
android:text="DIVE"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
Main Activity 2 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"
android:scaleType="centerCrop"
tools:context="com.example.goodvibes.helloworld.MainActivity">
<Button
android:id="#+id/Button2"
style="#style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:background="#drawable/timebutton"
android:fontFamily="serif-monospace"
android:text="Time Interval Between Dives"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/imageButton"
style="#android:style/Widget.ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:layout_marginTop="11dp"
android:adjustViewBounds="false"
android:background="#android:color/background_light"
android:cropToPadding="false"
android:tint="#android:color/background_dark"
app:srcCompat="?android:attr/actionModeCloseDrawable" />
</RelativeLayout>
Main Activity Java
package com.example.goodvibes.helloworld;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button but1;
public void init(){
but1= (Button)findViewById(R.id.button);
but1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent toy = new
Intent(MainActivity.this,activity_main_2.class);
startActivity(toy);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();{
}
}
}
Main Activity 2 Java
package com.example.goodvibes.helloworld;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ViewAnimator;
public class activity_main_2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.goodvibes.helloworld">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=".activity_main_2"></activity>
</application>
</manifest>
I had a lot of great people giving me answers I had to research. In the end here is what solved my problem: I inserted "android:onClick="ImageButton" under my button code in the xml fine THEN under the corresponding java file for my second activity I inserted:
public void ImageButton(View v)
{
// some code
finish();
}
Add this to your layout file:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_acashmemoreport"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:theme="#style/ToolbarColoredBackArrow" />
In your activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_acashmemoreport);
setSupportActionBar(toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
// Show menu icon
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
First of all you have to add a required toolbar and after making the toolbar you put the button inside the toolbar like following;
Update your xml layout file as,
<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" >
<ImageButton
android:id="#+id/btn_back_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back"
/>
</android.support.v7.widget.Toolbar>
Also include the below java code inside your Activity Class,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageButton back=findViewById(R.id.btn_back_toolbar);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this,AnotherActivity.class));
}
});
I was trying to use user input name for later use but Once i run the app this is closed by saying unfortunately your app has stopped. I have provided Main activity,Layout xml and android manifest codes. Please anyone answer this . I am newbie in android and cant figure out the error.
Main_activity
package rupakthapa.droiddynasty.com.interactivestory2;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import static android.R.attr.duration;
import static android.R.attr.name;
public class MainActivity extends AppCompatActivity {
private EditText mNameField;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameField = (EditText) findViewById(R.id.nameField);
mButton = (Button) findViewById(R.id.startButton);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String a= mNameField.getText().toString();
Context context = getApplicationContext();
int length = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, a, length);
toast.show();
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
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"
tools:context="rupakthapa.droiddynasty.com.interactivestory2.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:id="#+id/imageView"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="149dp"
android:contentDescription="startImage"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/nameField"
android:hint="Enter YOur Name"
android:layout_above="#+id/startButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:maxLength="30"
android:layout_marginLeft="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<Button
android:text="Start your adventure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/startButton"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest package="rupakthapa.droiddynasty.com.interactivestory2"
xmlns:android="http://schemas.android.com/apk/res/android">
<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"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
There is a problem here. You dont have EditText in your layout.
mNameField = (EditText) findViewById(R.id.nameField);
In your layout you should change TextView to EditText
<EditText
android:layout_width="match_parent"
android:id="#+id/nameField"
android:hint="Enter YOur Name"
android:layout_above="#+id/startButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:maxLength="30"
android:layout_marginLeft="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:textSize="16sp"/>
Running the app cause no problem but it shows a warning
Too many Output!
Surprisingly when I check my phone because I use it as my deployment output instead of a virtual device, there are 3 application that was installed. Here's the screen shot.
phone screenshot
Here's my Java Codes
MainActivity.Java
package com.faacts.jayson.faactsv004;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button;
button = (Button) findViewById(R.id.btnEnglish);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), Index.class);
startActivity(intent);
}
});
}
}
Splash.Java
package com.faacts.jayson.faactsv004;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* Created by Jayson on 5/22/2016.
*/
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final ImageView iv = (ImageView) findViewById(R.id.imageView);
final Animation an = AnimationUtils.loadAnimation(getBaseContext(), R.anim.rotate);
final Animation an2 = AnimationUtils.loadAnimation(getBaseContext(),R.anim.abc_fade_out);
iv.startAnimation(an);
an.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iv.startAnimation(an2);
finish();
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
Index.Java
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
/**
* Created by Jayson on 5/22/2016.
*/
public class Index extends AppCompatActivity {
EditText txtpassword;
CheckBox chkbox;
#Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.index);
Button button;
button = (Button) findViewById(R.id.btnRegister);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), Registration.class);
startActivity(intent);
}
});
txtpassword = (EditText) findViewById(R.id.txtPassword);
chkbox = (CheckBox) findViewById(R.id.chkBox);
chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// show password
txtpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
chkbox.setText("Reveal Password");
} else {
// hide password
txtpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
chkbox.setText("Hide Password");
}
}
});
}
}
Registration.Java
package com.faacts.jayson.faactsv004;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
/**
* Created by Jayson on 5/22/2016.
*/
public class Registration extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.gender_array,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
}
}
This is the example of my xml
activity_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"
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"
android:background="#33ccff"
android:clickable="false"
style="#style/AppTheme">
<ImageView
android:layout_width="600dp"
android:layout_height="700dp"
android:id="#+id/imageView3"
android:src="#drawable/welcomepage"
android:layout_marginBottom="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="#string/welcomepage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Choose your language:"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="165dp"
android:textColor="#003333"
android:textStyle="bold" />
<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/btnFilipino"
android:id="#+id/btnFilipino"
android:layout_alignTop="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="#drawable/bkg"
android:textColor="#000066"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/btnEnglish"
android:id="#+id/btnEnglish"
android:background="#drawable/bkg"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:textColor="#000066"
android:textSize="25sp"
android:textStyle="bold" />
</RelativeLayout>
index.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"
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=".Index"
android:background="#33ccff"
android:clickable="false"
style="#style/AppTheme">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33ccff">
<ImageView
android:layout_width="380dp"
android:layout_height="90dp"
android:layout_marginTop="50dp"
android:id="#+id/imgULogo"
android:layout_gravity="center_horizontal"
android:src="#drawable/upperlogo2"
android:contentDescription="#string/upperlogo" />
<EditText
android:layout_width="318dp"
android:layout_height="40dp"
android:layout_marginTop="40dp"
android:layout_gravity="center_horizontal"
android:id="#+id/txtUsername"
android:background="#ffffff"
android:hint="#string/username"
android:textColorHint="#color/dim_foreground_material_dark"
android:textColor="#000000"
android:inputType="text" />
<EditText
android:layout_width="318dp"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/txtPassword"
android:background="#ffffff"
android:hint="#string/password"
android:textColorHint="#color/dim_foreground_material_dark"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:layout_marginLeft="26dp"
android:text="#string/reveal_password"
android:id="#+id/chkBox" />
<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/login"
android:id="#+id/btnLogin"
android:layout_marginTop="90dp"
android:background="#drawable/bkg"
android:textColor="#000066"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/register"
android:id="#+id/btnRegister"
android:background="#drawable/bkg"
android:layout_marginTop="10dp"
android:textColor="#000066"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
Please hoping for your feedback Anyway, I am new in android studio.
Anyway, there are two xmls that I did not post I just only pasted the example two those are the registration.xml and splash.xml.
PROBLEM SOLVED: I put a android.intent.category.LAUNCHER instead of just a DEFAULT in every Java class on manifest declaration.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.faacts.jayson.faactsv004">
<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.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Index">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Registration">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
I have this problem that just seems strange.
I have this Android View with six buttons and when i click on one of them(the only one with the action defined apart from the exit button), it gives me a RuntimeError.
The Activity is declared on the manifest, and this is why i don't understand my error.
Can someone please help me with this.
I would really appreciate it :)
So here's my code:
package com.example.calendar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View createButton = findViewById(R.id.btn_create);
View deleteButton = findViewById(R.id.btn_delete);
View moveButton = findViewById(R.id.btn_move);
View searchButton = findViewById(R.id.btn_search);
View translateButton = findViewById(R.id.btn_translate);
View exitButton = findViewById(R.id.exit);
createButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.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.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_create:
Intent i = new Intent(this, CreateAppointment.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
My layout is like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="225dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin" >
<CalendarView
android:id="#+id/calendar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="2dip"
android:layout_marginTop="2dip"
android:stretchColumns="*" >
<TableRow>
<Button
android:id="#+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create" />
<Button
android:id="#+id/btn_viewEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/viewEdit" />
</TableRow>
<TableRow>
<Button
android:id="#+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/delete" />
<Button
android:id="#+id/btn_move"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/move" />
</TableRow>
<TableRow>
<Button
android:id="#+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/search" />
<Button
android:id="#+id/btn_translate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/translate" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/exit"/>
</LinearLayout>
And the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calendar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.calendar.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=".CreateAppointment"
android:theme="#android:style/Theme.Dialog"
android:label="#string/create" >
</activity>
</application>
</manifest>
You have to define correctly what kind of views ares using in onCreate()
View createButton = findViewById(R.id.btn_create);
for example:
since you are using a Button:
<Button
android:id="#+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create" />
Make the cast as button:
Button createButton = (Button)findViewById(R.id.btn_create);
try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createButton = (Button)findViewById(R.id.btn_create);
Button deleteButton = (Button)findViewById(R.id.btn_delete);
Button moveButton = (Button)findViewById(R.id.btn_move);
Button searchButton = (Button)findViewById(R.id.btn_search);
Button translateButton = (Button)findViewById(R.id.btn_translate);
Button exitButton = (Button)findViewById(R.id.exit);
createButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.setOnClickListener(this);
}