I'm a new Android developer working on the JustJava app on Udacity Beginners course. Using my phone to debug my code, when I clicked on the button to update price it gives the error: "Unfortunately JustJava has stopped".
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.justjava">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="justjava"
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>
</application>
MainActivity.java
import android.icu.text.NumberFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
display(2);
displayPrice(2 * 5);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView)
findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
/**
* *This method displays the given price on the screen.
*/
private void displayPrice(int number) {
TextView priceTextView = (TextView)
findViewById(R.id.price_text_view);
priceTextView.setText
(NumberFormat.getCurrencyInstance().format(number));
}
}
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.justjava.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="quantity"
android:textAllCaps="true"
android:padding="16dp" />
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16sp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:textColor="#android:color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:textAllCaps="true"
android:padding="16dp" />
<TextView
android:id="#+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NGN0"
android:textSize="16sp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:textColor="#android:color/black"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ORDER"
android:layout_margin="16dp"
android:onClick="submitOrder"/>
</LinearLayout>
Logcat error:
04-25 09:22:04.944 449-449/com.example.android.justjava E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.justjava, PID: 449
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4470)
at android.view.View$PerformClick.run(View.java:18773)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5341)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4470)
at android.view.View$PerformClick.run(View.java:18773)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5341)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: android.icu.text.NumberFormat
at com.example.android.justjava.MainActivity.displayPrice(MainActivity.java:49)
at com.example.android.justjava.MainActivity.submitOrder(MainActivity.java:33)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4470)
at android.view.View$PerformClick.run(View.java:18773)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5341)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
The error is rather specific. The root cause is at the end of the stacktrace :
Caused by: java.lang.NoClassDefFoundError: android.icu.text.NumberFormat
at com.example.android.justjava.MainActivity.displayPrice(MainActivity.java:49)
at com.example.android.justjava.MainActivity.submitOrder(MainActivity.java:33)
The classloader could have not found the android.icu.text.NumberFormat class definition you are using in your app.
You should check that the Android version installed on your phone is compatible with this class that is available only since the API 24 (Android 7.0).
https://developer.android.com/reference/android/icu/text/NumberFormat.html
Related
i am new to android studio and trying to get the my first app running. The hello world function works fine but the app closes whenever i press the send button in the second application.
The website tutorial i have based this off can be found here: https://developer.android.com/training/basics/firstapp/starting-activity
Here are my codes:
Mainactivity.java
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
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);
}
}
DisplayMessageActivity.java
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp">
<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.MyFirstApp">
<activity android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</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>
Any help is much appreciated!
Thanks
Josh
EDIT
Here is the error in logcat
2021-06-22 13:36:31.716 6345-6345/com.example.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 6345
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:7352)
at android.widget.TextView.performClick(TextView.java:14230)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7318)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27800)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7352)
at android.widget.TextView.performClick(TextView.java:14230)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7318)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27800)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:24)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7352)
at android.widget.TextView.performClick(TextView.java:14230)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7318)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27800)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
2021-06-22 13:36:31.777 6345-6345/com.example.myfirstapp I/Process: Sending signal. PID: 6345 SIG: 9
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".DisplayMessageActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="casual"
android:text="TextView"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#00BCD4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Cannot say causes the app to close without the logcat
but it could be because of it could not find the Edittext in the sendMessage method.
The id need to be the same in the xml and java files. In your case the edittext id edit_message
/** Called when the user taps the Send button */
public void sendMessage(View view) {
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);
}
<EditText
android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
There is your xml layout file.
I am very new to Android app making, but I'm able to understand java. The program which I'm making, should return relative frequency of characters that are in the text that person will write in the program (input text is meant to be written in EditText field with id inputText, and as soon as person will click button with id continueButton, onClick the program should continue with onClickFindRelevancy method). The code seems fine to me, but the problem is still occuring. I am able to input the text, but as soon as I click the button, then the app stops working and I just can't figure out what is wrong... What do I need to change in code (what is wrong), for the program to do what is intended?
I really tried to find a solution to my problem, but didn't find something like I have (just seeing the code snippets that someone else had problems with), so I hope that this topic won't be closed, because I tried and really didn't find the solution. Have been trying for some time now.
MainActivity.java
package com.example.<theprogrammnameihave>;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import java.util.Map;
import java.util.TreeMap;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickFindRelevancy(View view)
{
EditText inputText = (EditText) findViewById(R.id.inputText);
String input = inputText.getText().toString();
TextView result = (TextView) findViewById(R.id.result);
Map<Character, Float> k = new TreeMap<Character, Float>();
for (char b : input.toCharArray()) {
if (k.containsKey(b)) {
k.put(b, k.get(b) + 1);
}
else {
k.put(b, 1f);
}
}
for (char b : input.toCharArray()) {
float freq = k.get(b) / input.length();
result.setText(getString(b) + " " + freq);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/greetingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/greeting_message"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.109" />
<Button
android:id="#+id/continueButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="136dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="137dp"
android:layout_marginBottom="340dp"
android:onClick="onClickFindRelevancy"
android:text="#string/button_send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputText" />
<EditText
android:id="#+id/inputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="101dp"
android:layout_marginBottom="191dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/continueButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/greetingText" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="205dp"
android:layout_marginTop="418dp"
android:layout_marginEnd="206dp"
android:layout_marginBottom="294dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.<theprogrammnameihave>">
<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>
</application>
</manifest>
The error I get when the app stops working -
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kriptoprogramma, PID: 13645
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x54
at android.content.res.Resources.getText(Resources.java:338)
at android.content.res.Resources.getString(Resources.java:432)
at android.content.Context.getString(Context.java:556)
at com.example.kriptoprogramma.MainActivity.onClickFindRelevancy(MainActivity.java:36)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
You should use error stack trace, as suggested in comments. See this link.
The mistake is that you use getString method to convert a char to String.
In Android, this is the method of Activity (and Context) to get a string from resources (i. e. when you are localizing your app).
You should use String.valueOf(char) instead.
This question already has answers here:
How to fix Error inflating class null
(2 answers)
Closed 4 years ago.
Am new to Android programming and I decided to build a quiz app. I built the layout and wanted to set up a button to start a new activity which begins the quiz. I also set up the method in the MainActivity correctly but when I run the app and click on the button, the app crashes and leaves a ton of error messages on logcat.
Any help will be appreciated. Below are the codes.
XML Code:
<Button
android:id="#+id/button_view"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="#id/name_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/button_round"
android:fontFamily="sans-serif-light"
android:inputType="textCapWords"
android:text="Go!"
android:onClick="startQuestionOne"
android:textSize="24dp" />
MainActivity
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void startQuestionOne(View view) {
Intent myIntent = new Intent(LoginActivity.this, QuestionOneActivity.class);
startActivity(myIntent);
}
}
Logcat Message
06-25 18:32:34.259 13705-13705/com.example.android.udacityquizapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.udacityquizapp, PID: 13705
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.udacityquizapp/com.example.android.udacityquizapp.QuestionOneActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class null
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5279)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:99)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5279)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
This is the full 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/gradient"
tools:context=".LoginActivity">
<ImageView
android:id="#+id/udacity_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="#drawable/udacity" />
<TextView
android:id="#+id/textview_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/udacity_logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:fontFamily="sans-serif-ligh"
android:text="Udacity Quiz App"
android:textColor="#faebd7"
android:textSize="36dp"
android:textStyle="bold" />
<EditText
android:id="#+id/name_view"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_below="#id/textview_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/edit_round"
android:drawableLeft="#drawable/ic_action_user"
android:ems="10"
android:fontFamily="sans-serif-light"
android:hint="Name"
android:inputType="textCapWords"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="16dp" />
<Button
android:id="#+id/button_view"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="#id/name_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/button_round"
android:fontFamily="sans-serif-light"
android:inputType="textCapWords"
android:text="Go!"
android:onClick="startQuestionOne"
android:textSize="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif-light"
android:text="powered by: #William"
android:textColor="#faebd7" />
</RelativeLayout>
Above is the XML for LoginActivity which contains a button. When the button is clicked, its supposed to start a new activity called QuestionOneActivity, but instead, the app crashes.
LoginActivity
package com.example.android.udacityquizapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void startQuestionOne(View view) {
Intent myIntent = new Intent(LoginActivity.this,
QuestionOneActivity.class);
startActivity(myIntent);
}
}
firstly bind Button in onCreate() as
Button b = (Button) findViewById(R.id.button_view);
then check if your activity QuestionOneActivity is declared in manifest file
after that check QuestionOneActivity activity xml file for error if have any...
Thanks clay_to_n, based on your suggestions, i found out that i used a lower case 'v' for my View tag. When i adjusted it, the app runs as expected.
I am attempting to concatenate strings in Android using two activities. Every time I enter two string and hit the button to run the app crashes on the emulator. Can you help me find what I am doing wrong here? My code and the error are below.
Main Activity (Activity 1)
package com.example.tristan.a1p1n1;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.tristan.a1p1n1.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DICS.class);
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
String message = editText.getText().toString() + editText2.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}}
DICS (Activity 2)
package com.example.tristan.a1p1n1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by Tristan on 2/11/2017.
*/
public class DICS extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dics);
//TextView mTextView = (TextView)findViewById(R.id.textView2);
// mTextView.setText(getIntent().getStringExtra("message"))
}
}
My Error Message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tristan.a1p1n1, PID: 3082
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.tristan.a1p1n1/com.example.tristan.a1p1n1.DICS}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4507)
at android.app.Activity.startActivity(Activity.java:4475)
at com.example.tristan.a1p1n1.MainActivity.sendMessage(MainActivity.java:41)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
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:id="#+id/activity_main"
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.tristan.a1p1n1.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter a String"
android:ems="10"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter a String"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp"
android:layout_marginTop="13dp" />
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp"
android:id="#+id/button"
android:onClick="sendMessage" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tristan.a1p1n1">
<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><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
dics.xml
<?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">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2" />
</LinearLayout>
your Button Xml should be:
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp"
android:id="#+id/button"
android:onClick="sendMessage" /> //change here
Try this: in your MainActivity
String message = editText.getText().toString() + editText2.getText().toString();
intent.putExtra("message", message);
Now in Dics activity:
TextView mTextView = (TextView)findViewById(R.id.textView2);
mTextView.setText(getIntent().getStringExtra("message")); //the key ID should match
This error has appeared on this forum millions of time but I really could not find the answer to resolve this error! I am using Navigation drawer with fragments, so far I have incorporated(I think I have) a countdown timer and a google map. I don't know what the problem is I have tried "almost" every solution mentioned on the internet for this error.
ERROR:
01-26 15:10:47.923: E/AndroidRuntime(3731): FATAL EXCEPTION: main
01-26 15:10:47.923: E/AndroidRuntime(3731): Process: com.example.TheWeddingApp, PID: 3731
01-26 15:10:47.923: E/AndroidRuntime(3731): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.TheWeddingApp/com.example.TheWeddingApp.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.navigationdrawer.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.access$800(ActivityThread.java:139)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.os.Handler.dispatchMessage(Handler.java:102)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.os.Looper.loop(Looper.java:136)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.main(ActivityThread.java:5086)
01-26 15:10:47.923: E/AndroidRuntime(3731): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 15:10:47.923: E/AndroidRuntime(3731): at java.lang.reflect.Method.invoke(Method.java:515)
01-26 15:10:47.923: E/AndroidRuntime(3731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-26 15:10:47.923: E/AndroidRuntime(3731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-26 15:10:47.923: E/AndroidRuntime(3731): at dalvik.system.NativeStart.main(Native Method)
01-26 15:10:47.923: E/AndroidRuntime(3731): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.navigationdrawer.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
01-26 15:10:47.923: E/AndroidRuntime(3731): at com.example.TheWeddingApp.MainActivity.onCreate(MainActivity.java:54)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.Activity.performCreate(Activity.java:5248)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
01-26 15:10:47.923: E/AndroidRuntime(3731): ... 11 more
01-26 15:10:47.923: E/AndroidRuntime(3731): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.navigationdrawer.FragmentOne" on path: DexPathList[[zip file "/mnt/asec/com.example.TheWeddingApp-1/pkg.apk"],nativeLibraryDirectories=[/mnt/asec/com.example.TheWeddingApp-1/lib, /vendor/lib, /system/lib]]
01-26 15:10:47.923: E/AndroidRuntime(3731): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-26 15:10:47.923: E/AndroidRuntime(3731): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
01-26 15:10:47.923: E/AndroidRuntime(3731): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
01-26 15:10:47.923: E/AndroidRuntime(3731): ... 16 more
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.TheWeddingApp"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.TheWeddingApp.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.TheWeddingApp.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.TheWeddingApp.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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBTbyjkzYDeEFGsljSoLBr54riAjhWCVJg" />
</application>
</manifest>
MainActivity.java
package com.example.TheWeddingApp;
import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
final String[] data ={"Home","Venue","Gallery","Meet the Groom","Meet `the Bride"};`
final String[] fragments ={
"com.example.navigationdrawer.FragmentOne",
"com.example.navigationdrawer.FragmentTwo",
"com.example.navigationdrawer.FragmentThree",
"com.example.navigationdrawer.FragmentFour",
"com.example.navigationdrawer.FragmentFive"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.drawer);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
#Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
tx.commit();
}
});
drawer.closeDrawer(navList);
}
});
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
tx.commit();
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ff0000"
android:choiceMode="singleChoice"/>
</android.support.v4.widget.DrawerLayout>
FragmentOne.java
package com.example.TheWeddingApp;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FragmentOne extends Fragment {
private TextView tvDay, tvHour, tvMinute, tvSecond, tvEvent;
private LinearLayout linearLayout1, linearLayout2;
private Handler handler;
private Runnable runnable;
public static Fragment newInstance(Context context) {
FragmentOne f = new FragmentOne();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);
getActivity().setContentView(R.layout.fragment_one);
initUI();
countDownStart();
return root;
}
#SuppressLint("SimpleDateFormat")
private void initUI() {
linearLayout1 = (LinearLayout) getActivity().findViewById(R.id.ll1);
linearLayout2 = (LinearLayout) getActivity().findViewById(R.id.ll2);
tvDay = (TextView) getActivity().findViewById(R.id.txtTimerDay);
tvHour = (TextView) getActivity().findViewById(R.id.txtTimerHour);
tvMinute = (TextView) getActivity().findViewById(R.id.txtTimerMinute);
tvSecond = (TextView) getActivity().findViewById(R.id.txtTimerSecond);
tvEvent = (TextView) getActivity().findViewById(R.id.tvevent);
}
public void countDownStart() {
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
// Here Set your Event Date
Date futureDate = dateFormat.parse("2016-2-19");
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
tvDay.setText("" + String.format("%02d", days));
tvHour.setText("" + String.format("%02d", hours));
tvMinute.setText("" + String.format("%02d", minutes));
tvSecond.setText("" + String.format("%02d", seconds));
} else {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
tvEvent.setText("Android Event Start");
handler.removeCallbacks(runnable);
// handler.removeMessages(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 0);
}
}
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#000000"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="#+id/tvevent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:singleLine="true"
android:text="Android Event Start"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="#000000"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="horizontal"
android:visibility="visible" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/txtTimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="#+id/txt_TimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Days"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/txtTimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="#+id/txt_TimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Hour"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/txtTimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="#+id/txt_TimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Minute"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/txtTimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="#+id/txt_TimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Second"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
FragmentTwo.java
package com.example.TheWeddingApp;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class FragmentTwo extends Fragment {
private GoogleMap googleMap;
public static Fragment newInstance(Context context) {
FragmentTwo f = new FragmentTwo();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_two, null);
getActivity().setContentView(R.layout.fragment_two);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
return root;
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
#Override
public void onResume() {
super.onResume();
initilizeMap();
}
}
fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
you are using
final String[] fragments ={
"com.example.navigationdrawer.FragmentOne",
"com.example.navigationdrawer.FragmentTwo",
"com.example.navigationdrawer.FragmentThree",
"com.example.navigationdrawer.FragmentFour",
"com.example.navigationdrawer.FragmentFive"};
to instantiate your Fragments in MainActivity. There you are providing the a full qualified path to your Fragments that, accordingly to the stacktrace, doesn't exits. The package of your Fragments is com.example.TheWeddingApp. You might what to change the String[] to reflect the real path. E.g.
final String[] fragments ={
"com.example.TheWeddingApp.FragmentOne",
"com.example.TheWeddingApp.FragmentTwo",
"com.example.TheWeddingApp.FragmentThree",
"com.example.TheWeddingApp.FragmentFour",
"com.example.TheWeddingApp.FragmentFive"};