Hi I have an app written that goes from a login page, to another activity with a progress bar, then to another activity with a tab host. If I get rid of the loading page and go from login to the tab host works fine, but if I try to go from login to loading the app stops and says 'Unfortunately application has stopped.' I have checked the logcat and see that there is a null pointer error for the Loading page, but I can't see why. In the intent to go from login to loading I call loading correctly, and the set content view in Loading matches its xml loading_main. Here is my code below, thannks:
Main login activity:
package com.example.loginscreen;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
}
public void login(View view){
if(username.getText().toString().equals("mara") &&
password.getText().toString().equals("mara")){
Toast.makeText(getApplicationContext(), "Login Successful!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this,Loading.class));
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));
if(counter==0){
login.setEnabled(false);
}
}
}
#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;
}
}
Loading activity:
package com.example.loginscreen;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class Loading extends Activity {
Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_main);
addListenerOnButton();
}
public void addListenerOnButton() {
btnStartProgress = (Button) findViewById(R.id.button1);
btnStartProgress.setOnClickListener( // <== This is line 33
new OnClickListener() {
#Override
public void onClick(View v) {
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("Searching for Driver Card...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//reset progress bar status
progressBarStatus = 0;
//reset filesize
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// driver card is found
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
});
startActivity(new Intent(Loading.this,LinkTabs.class));
}
});
}
// file download simulator
public int doSomeTasks() {
while (fileSize <= 1000000) {
fileSize++;
if (fileSize == 100000) {
return 10;
} else if (fileSize == 200000) {
return 20;
} else if (fileSize == 300000) {
return 30;
}
}
return 100;
}
}
Activity LinkTabs to link all tabs in tab host:
package com.example.loginscreen;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
#SuppressWarnings("deprecation")
public class LinkTabs extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.link_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// First tab
Intent intentAndroid = new Intent().setClass(this, HomePage.class);
TabSpec tabSpecHomePage = tabHost
.newTabSpec("HomePage")
.setIndicator("", ressources.getDrawable(R.drawable.overview))
.setContent(intentAndroid);
// Second tab
Intent intentApple = new Intent().setClass(this, HomePage2.class);
TabSpec tabSpecHomePage2 = tabHost
.newTabSpec("HomePage2")
.setIndicator("", ressources.getDrawable(R.drawable.card_summary))
.setContent(intentApple);
// Third tab
Intent intentWindows = new Intent().setClass(this, HomePage3.class);
TabSpec tabSpecHomePage3 = tabHost
.newTabSpec("HomePage3")
.setIndicator("", ressources.getDrawable(R.drawable.details))
.setContent(intentWindows);
// add all tabs
tabHost.addTab(tabSpecHomePage);
tabHost.addTab(tabSpecHomePage2);
tabHost.addTab(tabSpecHomePage3);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(2);
}
}
Tab activities:
package com.example.loginscreen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HomePage extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Overview");
setContentView(textview);
}
}
package com.example.loginscreen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HomePage2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Card Summary");
setContentView(textview);
}
}
package com.example.loginscreen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HomePage3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Details");
setContentView(textview);
}
}
Here are the xml files:
//activity_main.xml
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_marginLeft="32dp"
android:layout_toRightOf="#+id/textView2"
android:ems="10"
tools:ignore="TextFields" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginTop="40dp"
android:layout_toLeftOf="#+id/editText1"
android:hint="Password"
android:text="#string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/button1"
android:text="TextView"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignParentTop="true"
android:layout_marginTop="155dp"
android:hint="Username"
android:text="#string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentTop="true"
android:src="#drawable/tranzlogo1" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_marginTop="26dp"
android:layout_toLeftOf="#+id/textView5"
android:text="#string/attempts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/textView4" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"
android:onClick="login"
android:text="#string/Login" />
</RelativeLayout>
//fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.loginscreen.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/tranzlogo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:src="#drawable/overview"
tools:ignore="ContentDescription" />
</RelativeLayout>
//link_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.loginscreen.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/tranzlogo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:src="#drawable/overview"
tools:ignore="ContentDescription" />
</RelativeLayout>
//loading_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
</LinearLayout>
Here is loading_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
</LinearLayout>
Here is the mainfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.loginscreen.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=".HomePage" />
<activity android:name=".HomePage2" />
<activity android:name=".HomePage3" />
<activity android:name=".Loading"/>
<activity android:name=".LinkTabs"/>
</application>
</manifest>
And the logcat output:
06-20 10:22:15.289: W/dalvikvm(29524): threadid=1: thread exiting with uncaught exception (group=0x40cca318)
06-20 10:22:15.299: E/AndroidRuntime(29524): FATAL EXCEPTION: main
06-20 10:22:15.299: E/AndroidRuntime(29524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginscreen/com.example.loginscreen.Loading}: java.lang.NullPointerException
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.access$600(ActivityThread.java:134)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.os.Looper.loop(Looper.java:137)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.main(ActivityThread.java:4744)
06-20 10:22:15.299: E/AndroidRuntime(29524): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 10:22:15.299: E/AndroidRuntime(29524): at java.lang.reflect.Method.invoke(Method.java:511)
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-20 10:22:15.299: E/AndroidRuntime(29524): at dalvik.system.NativeStart.main(Native Method)
06-20 10:22:15.299: E/AndroidRuntime(29524): Caused by: java.lang.NullPointerException
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.example.loginscreen.Loading.addListenerOnButton(Loading.java:33)
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.example.loginscreen.Loading.onCreate(Loading.java:26)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.Activity.performCreate(Activity.java:5008)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
06-20 10:22:15.299: E/AndroidRuntime(29524): ... 11 more
You have referenced in Your LoadingMain activity the following layout with setContentView:
setContentView(R.layout.loading_main);
But this Layout does not have any button1. Instead, You are trying to reference a button from another layout, from activity_main.xml
btnStartProgress = (Button) findViewById(R.id.button1);
This button1 is inside the activity_main.xml and You cannot reference a button in a layout that is not attached per setContentView. So What You have to do is, add a button to the loading_main.xml, BUT, don´t give it the same id like in the activity main "button1". Sure, it works, to give same ids as long as the right layout is referenced, but id´s should be unique. If the button in the activity_layout should remain, give the id for your button inside loading_main.xml another name, maybe android:id="#+id/loading_main_button_1".
Clearly the error is in xml, you have not added the button here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
</LinearLayout>
Try adding the following in your loading_main.xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
The complete thing should look like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
NullPointerExcception
it causes because using null object or not defining value to object.
In Loading.java you have used
setContentView(R.layout.loading_main);
that don't have button1
add button1 in loading_main.xml it or Change layout in setContentView(R.layout.other_layout);
Related
objectName.java
package randomexcessor.foutuneteller;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class objectName extends AppCompatActivity {
EditText objectInput;
FloatingActionButton actionButton;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_object_name);
// An intent received and hence been written bellow the Edit_text input to
be shown as consideration.
Intent toAgeInput = getIntent();
final String name = toAgeInput.getStringExtra("users name");
final String display = toAgeInput.getStringExtra("alphabet");
TextView show = (TextView) findViewById(R.id.disclaimer);
show.setText("Please type the name starting with the alphabet, Capital '
" + display+ " '");
objectInput = (EditText) findViewById(R.id.inputName);
actionButton = (FloatingActionButton) findViewById(R.id.outputButton);
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.custom_toast_container));
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*condition layout which allows user to dial only an alphabet
starting with random selected alphabet from baseInt activity
input.*/
if (!objectInput.getText().toString().matches(display+ "[a-zA-
Z]+"))
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("You have to Start with 'Capital " +
display+"'.");
Toast toast = new Toast(objectName.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else if (!objectInput.getText().toString().matches("[a-zA-Z]+"))
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("Your name must be in words");
Toast toast = new Toast(objectName.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else
{
Intent openAge = new Intent(objectName.this,
ageInput.class);
String input = objectInput.getText().toString();
openAge.putExtra("alphabet", display);//alphabet to use
openAge.putExtra("users name", name);//name of the user.
openAge.putExtra("belonging", input);//the object name
provided in this class.
startActivity(openAge);//running activity.
}
}
});
}}
This file is almost working great but whenever I click on the next button it crashes the app
activity_objectName.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/object1bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/appbar2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="45dp"
app:srcCompat="#drawable/type15"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/appbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5sp"
android:background="#drawable/edit_background"
android:padding="2dp"
android:weightSum="1">
<EditText
android:id="#+id/inputName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/edit_text"
android:ems="10"
android:inputType="text"
android:padding="10dp"
android:textSize="18sp"
android:layout_margin="3dp"
android:layout_weight="1.02"
android:hint="#string/Object"
android:textColor="#A52A2A"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/outputButton"
android:layout_marginEnd="10dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:backgroundTint="#32CD32"
app:elevation="2dp"
app:fabSize="normal"
app:srcCompat="#drawable/ic_keyboard_arrow_right_white_24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/appbar2"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/button"
tools:ignore="UnknownIdInLayout">
<TextView
android:id="#+id/disclaimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Main problem is here I'm not able to go to this file "ageInput.java",
activity_ageInput.xml
package randomexcessor.foutuneteller;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ageInput extends AppCompatActivity {
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_age_input);
final EditText ageFind = (EditText) findViewById(R.id.inputAge);
FloatingActionButton ageButton = (FloatingActionButton)
findViewById(R.id.ageButton);
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.custom_toast_container));
ageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ageFind.getText().toString().isEmpty())
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("Please type your age or System error.");
text = (TextView) layout.findViewById(R.id.errorReport);
Toast toast = new Toast(ageInput.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else if(ageFind.getText().length() > 2)
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("Your age cannot be " +
ageFind.getText().toString() +"!!");
text = (TextView) layout.findViewById(R.id.errorReport);
Toast toast = new Toast(ageInput.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else
{
Intent runResult = getIntent();
final String alpha = runResult.getStringExtra("alphabet");
final String name = runResult.getStringExtra("users name");
final String belong = runResult.getStringExtra("belonging");
Intent finalPage = new Intent(ageInput.this, result.class);
finalPage.putExtra("alphabet", alpha);
finalPage.putExtra("users name", name);
finalPage.putExtra("belonging", belong);
finalPage.putExtra("users age",
ageFind.getText().toString());
startActivity(finalPage);
}
}
});
}
}
activity_ageInput.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/agefact2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
app:srcCompat="#drawable/type13"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="6sp"
android:background="#drawable/edit_background"
android:padding="2dp"
android:weightSum="1">
<EditText
android:id="#+id/inputAge"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/edit_text"
android:ems="10"
android:hint="#string/age"
android:inputType="text"
android:padding="10dp"
android:textSize="18sp"
android:layout_margin="3dp"
android:layout_weight="1.02"
android:textColor="#color/colorAccent"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/ageButton"
android:layout_marginEnd="10dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:backgroundTint="#32CD32"
app:elevation="2dp"
app:fabSize="normal"
app:rippleColor="#7FFF00"
app:srcCompat="#drawable/ic_keyboard_arrow_right_white_24dp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
edit_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="1dp">
<stroke
android:color="#696969"
android:width="2dp"/>
<solid android:color="#FFFFFF"></solid>
<corners
android:radius="25dp" />
</shape>
edit_text.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
</shape>
crash report
07-02 13:29:40.966 14586-14586/randomexcessor.foutuneteller E/AndroidRuntime: FATAL EXCEPTION: main
Process: randomexcessor.foutuneteller, PID: 14586
java.lang.OutOfMemoryError: Failed to allocate a 19743564 byte allocation with 15898356 free bytes and 15MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2384)
at android.content.res.Resources.getDrawable(Resources.java:787)
at android.content.Context.getDrawable(Context.java:403)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:54)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:66)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at randomexcessor.foutuneteller.ageInput.onCreate(ageInput.java:22)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
The first level of your crash report shows an OOM, which means OutOfMemory. Any Bitmap that you put inside activity_ageInput.xml or that maybe you used in your Toast´s layout is too big (or all bitmaps). You should consider to reduce the size of your bitmaps. Read this thread about handling bitmaps:
https://developer.android.com/topic/performance/graphics/index.html
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.
I am beginner in Android Studio. I am trying to run my app on my mobile, but when i try to run my application on my mobile it shows
Unfortunately app has been stopped
Here is the error showed in Logcat android monitor.
10-23 23:19:25.882 18014-18014/com.example.dhara.codesprint E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dhara.codesprint, PID: 18014
java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.example.dhara.codesprint/com.example.dhara.codesprint.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5372)
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:970)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
at com.example.dhara.codesprint.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5258)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5372)
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:970)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
at dalvik.system.NativeStart.main(Native Method)
Below is the code for MainActivity.java
package com.example.dhara.codesprint;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
EditText text7;
Button checkAnswer;
Button reset;
//private WordDictionary dictionary;
private String currentWord;
private ArrayList<String> words;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AssetManager assetManager = getAssets();
try {
InputStream inputStream = assetManager.open("words.txt");
} catch (IOException e) {
Toast toast = Toast.makeText(this, "Could not load dictionary", Toast.LENGTH_LONG);
toast.show();
}
text1 = (TextView) findViewById(R.id.textView1);
text2 = (TextView) findViewById(R.id.textView2);
text3 = (TextView) findViewById(R.id.textView3);
text4 = (TextView) findViewById(R.id.textView4);
text5 = (TextView) findViewById(R.id.textView5);
text6 = (TextView) findViewById(R.id.textView6);
text7 = (EditText) findViewById(R.id.edittext);
checkAnswer = (Button) findViewById(R.id.button);
reset = (Button) findViewById(R.id.button2);
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
checkAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dhara.codesprint">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Codesprint"
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>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android: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"
android:background="#color/background"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:layout_marginTop="14dp"
android:id="#+id/imageView1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView2"
android:layout_alignTop="#+id/imageView3"
android:layout_alignLeft="#+id/imageView5"
android:layout_alignStart="#+id/imageView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView3"
android:layout_alignTop="#+id/imageView1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:layout_marginTop="27dp"
android:id="#+id/imageView4"
android:layout_below="#+id/imageView1"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignStart="#+id/imageView1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView5"
android:layout_alignTop="#+id/imageView4"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView6"
android:layout_alignTop="#+id/imageView5"
android:layout_alignLeft="#+id/imageView3"
android:layout_alignStart="#+id/imageView3" />
<EditText
android:text="Your Answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:id="#+id/edittext"
android:textSize="20sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColorLink="?attr/actionMenuTextColor" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:id="#+id/textView2"
android:textAlignment="center"
android:textStyle="normal|bold"
android:layout_alignTop="#+id/imageView2"
android:layout_alignLeft="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignRight="#+id/imageView3"
android:layout_alignEnd="#+id/imageView3"
android:id="#+id/textView3"
android:layout_alignLeft="#+id/imageView3"
android:layout_alignStart="#+id/imageView3"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView4"
android:layout_alignRight="#+id/imageView4"
android:layout_alignEnd="#+id/imageView4"
android:layout_marginTop="30dp"
android:id="#+id/textView4"
android:layout_alignLeft="#+id/imageView4"
android:layout_alignStart="#+id/imageView4"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView4"
android:layout_alignRight="#+id/imageView5"
android:layout_alignEnd="#+id/imageView5"
android:id="#+id/textView5"
android:layout_alignLeft="#+id/imageView5"
android:layout_alignStart="#+id/imageView5"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView5"
android:layout_alignRight="#+id/imageView6"
android:layout_alignEnd="#+id/imageView6"
android:id="#+id/textView6"
android:layout_alignLeft="#+id/imageView6"
android:layout_alignStart="#+id/imageView6"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignRight="#+id/imageView1"
android:layout_alignEnd="#+id/imageView1"
android:id="#+id/textView1"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignStart="#+id/imageView1"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<Button
android:text="Check Your Answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:id="#+id/button"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light"
android:background="#color/colorPrimary"
android:layout_below="#+id/edittext"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="27dp"
android:id="#+id/button2"
android:background="#color/colorPrimary"
android:textSize="14sp"
tools:textColor="#android:color/background_light" />
</RelativeLayout>
I have tried some solution from web but it is not working.
You layout does not have a Toolbar, and yet you are calling the following:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Either remove these calls or add a Toolbar to your layout.
For the latter to work you also need to use (or derive from) one of the AppCompat themes that do not have an ActionBar, Theme.AppCompat.Light.NoActionBar for example.
So in your styles.xml you would have something like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
You can't initiate your view elements (TextView, Buttons etc) before setting out the layout xml which for your case is R.layout.activity_main file.
package com.example.dhara.codesprint;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//private WordDictionary dictionary;
private String currentWord;
private ArrayList<String> words;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text1 = (TextView) findViewById(R.id.textView1);
TextView text2 = (TextView) findViewById(R.id.textView2);
TextView text3 = (TextView) findViewById(R.id.textView3);
TextView text4 = (TextView) findViewById(R.id.textView4);
TextView text5 = (TextView) findViewById(R.id.textView5);
TextView text6 = (TextView) findViewById(R.id.textView6);
TextView text7 = (TextView) findViewById(R.id.edittext);
Button checkAnswer = (Button) findViewById(R.id.button);
Button reset = (Button) findViewById(R.id.button2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AssetManager assetManager = getAssets();
try {
InputStream inputStream = assetManager.open("words.txt");
} catch (IOException e) {
Toast toast = Toast.makeText(this, "Could not load dictionary", Toast.LENGTH_LONG);
toast.show();
}
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
checkAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
Please initialize your layouts element in onCreate() because when your activity get start it calls onCreate() method ,but here you are calling your layout from your onCreate() method but after that you are not initializing your elements ,also check that whether you have added the name of activity in your manifest or not ,please read and implement carefully ,hope it ll help you.
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"};
Answered(at bottom)
So I have been trying to get my button to allow me to go to the next screen.
I am programming in eclipse.
It is a basic login button to allow me to go to the login screen.
I apologize for the long post, but I feel that giving all the information I have right now is good.
I am wondering if it is my device I am using it to run on when I press play.
The error at the bottom of my logcat is:
(The first few lines of logcat)
11-06 18:08:59.973: PID 324 TID 324 D/AndroidRuntime(324): Shutting down VM
11-06 18:08:59.973: W/dalvikvm(324): threadid=1: thread exiting with uncaught exception
(group=0x4001d800)
11-06 18:08:59.983: E/AndroidRuntime(324): FATAL EXCEPTION: main
11-06 18:08:59.983: E/AndroidRuntime(324): android.content.ActivityNotFoundException: Unable to
find explicit activity class {com.example.coffeeshop/com.example.coffeeshop.Login}; have you
declared this activity in your AndroidManifest.xml?
Those are the first few lines as it goes to error.
So my res/layout/activity_main code:
(Please note that the second button for "Create account" is not going to be an issue I am trying to get the login to go to the login screen.)
<?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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".Main"
android:background="#15BCA8"
android:id="#+id/activity_main">
<Button
android:id="#+id/createbtn"
android:layout_width="2200dp"
android:layout_height="70dp"
android:padding="10dp"
android:textSize="25sp"
android:textStyle="bold|normal"
android:text="#string/create_account"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="44dp"
android:background="#drawable/roundedbutton" />
<Button
android:id="#+id/loginbtn"
android:layout_width="2200dp"
android:layout_height="80dp"
android:layout_above="#+id/createbtn"
android:layout_alignStart="#+id/createbtn"
android:layout_alignLeft="#+id/createbtn"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="29dp"
android:padding="10dp"
android:text="#string/log_in"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold|normal"
android:typeface="sans"
android:background="#drawable/roundedbutton"
/>
</RelativeLayout>
And this layout is suppose to lead to the login.xml. Also, the roundedbutton will be posted at the end, but I don't think it's an issue.
login.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:background="#15BCA8"
android:id="#+id/loginlayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp"
android:text="Login"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/button"
android:textColor="#000000"
android:background="#15BCA8"
android:layout_marginTop="65dp"
android:layout_toStartOf="#+id/textView"
android:layout_below="#+id/textView"
android:layout_toLeftOf="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/button2"
android:textColor="#000000"
android:background="#15BCA8"
android:layout_marginTop="58dp"
android:layout_below="#+id/editText"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_below="#+id/button"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2"
android:background="#FFFFFF" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2"
android:background="#FFFFFF" />
<Button
android:id="#+id/sbmtbtn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/editText2"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/editText2"
android:layout_marginTop="20dp"
android:background="#drawable/roundedbutton"
android:text="Submit"
android:textSize="24sp" />
</RelativeLayout>
So this seems relatively fine, yeah?
Here is my MainActivity.java class:
package com.example.coffeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.loginbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Login.class);
startActivity(intent);
}
});
}
}
And should link me to my login screen, coded here:
package com.example.coffeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Login extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.sbmtbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MainMenu.class);
startActivity(intent);
}
});
}
}
And for fun, here is my "roundedbutton.xml" file being called upon by my xml files.
Which shouldn't have an issue here.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- solid android:color="#834672"-->
<gradient android:startColor="#834672"
android:endColor="#718C93"
android:angle="270" />
<stroke android:width="5px" android:color="#06302F" />
<corners android:radius="10dp"/>
</shape>
Now I can not understand why my program crashes.
Ok. I now have an answer that has worked out for me. I was not aware of the manifest, which was instantly solved with the code that "fast snail" left in his answer.
based on your logcat, the problem is that you haven't defined your activity in manifest file.
The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code. before the app runs the system must know which activities are there in your app, and it knows via manifest file. and if you miss any activity in manifest file then system assumes that the activity doesnt exist. now, when app runs it points to some activity that is not recognized by the system. hence, it crashes. you can read more about android manifest file here http://developer.android.com/guide/topics/manifest/manifest-intro.html
i cannot upvote else i would go with #fast snail's answer,i think that will solve your problem.
well
ActivityNotFoundException
that mean you have not declare this activity in AndroidManifest file.you need to declare all activities in manifest file
error exactly says you the problem
find explicit activity class {com.example.coffeeshop/com.example.coffeeshop.Login}; have you
declared this activity in your AndroidManifest.xml?
add this line to manifest file
<activity android:name=".Login"></activity>
finaly manifest should looks like// example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.coffeeshop">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity" android:label="#string/app_name"></activity>
<activity android:name=".Login"></activity> <!--add this line -->
</application>
</manifest>