Hi I am fairly new to android development and generally seem to be understanding this, I've been following a tutorial on youtube and it has all worked fine until now I keep getting the error message that the app has stopped unexpectedly.
right now I have two Java files:
Game.java
MainMenu.java
And I have four XML files:
activity_game.xml
activity_main_menu.xml
pause_menu.xml
pause.xml
Here is the code for Game.java:
package com.example.deepseadiver;
import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Game extends ActionBarActivity {
View Pause;
View Pause_Menu;
RelativeLayout Rel_main_game;
//Option for user selecting continue
OnClickListener Continue_List = new OnClickListener() {
#Override
public void onClick(View v) {
//Make the pause menu invisible
Pause_Menu.setVisibility(View.GONE);
//Make the game Visible
Pause.setVisibility(View.VISIBLE);
}
};
//Option for user selecting Main Menu
OnClickListener Main_Menu_List = new OnClickListener() {
#Override
public void onClick(View v) {
Game.this.finish();
}
};
OnClickListener Pause_Click = new OnClickListener() {
#Override
public void onClick(View v) {
Pause.setVisibility(View.GONE);
Pause_Menu.setVisibility(View.VISIBLE);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Rel_main_game = (RelativeLayout) findViewById(R.id.Game_Screen);
//Gets the size of the device Screen
DisplayMetrics Size = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(Size);
#SuppressWarnings("unused")
//Sets the screen Width & Height in pixels
final int Screen_Height = Size.heightPixels;
final int Screen_Width = Size.widthPixels;
//Sets the Pause button Layout
#SuppressWarnings("static-access")
LayoutInflater myInflater = (LayoutInflater) getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
Pause = myInflater.inflate(R.layout.pause, null, false);
Pause.setX(Screen_Width - 250);
Pause.setY(0);
Rel_main_game.addView(Pause);
Pause.setOnClickListener(Pause_Click);
//Sets the Height and Width
Pause.getLayoutParams().height=250;
Pause.getLayoutParams().width=250;
Pause = myInflater.inflate(R.layout.pause_menu, null, false);
Rel_main_game.addView(Pause_Menu);
Pause_Menu.setVisibility(View.GONE);
ImageView Continue = (ImageView)Pause_Menu.findViewById(R.id.Continue);
ImageView Return_Main = (ImageView)Pause_Menu.findViewById(R.id.Return_Main);
Continue.setOnClickListener(Continue_List);
Return_Main.setOnClickListener(Main_Menu_List);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the code for MainMenu.java:
package com.example.deepseadiver;
//Imports the Required Android libaries
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
//Declaration of the Main Menu
public class MainMenu extends ActionBarActivity {
//Creates the Code Views
MediaPlayer MainMenuMusic;
RelativeLayout Start;
ImageView ImageButton;
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
//Creates a code representation of a graphical object on activity_main_menu
Start = (RelativeLayout) findViewById(R.id.Button_Start);
ImageButton = (ImageView) findViewById(R.id.Image_Button);
txt = (TextView) findViewById(R.id.Text_Start);
//Imprts a Custom Font
Typeface Adventure = Typeface.createFromAsset(getAssets(), "Adventure.ttf");
txt.setTypeface(Adventure);
//Detects the user touch on Main Menu and changes button appearance
Start.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
//Detects a user Click on Main Menu and starts the next activity
Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Start_Game = new Intent(MainMenu.this, Game.class);
startActivity(Start_Game);
}
});
}
#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, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the code for activity_game.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/Game_Screen"
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.deepseadiver.Game" >
</RelativeLayout>
Here is the code for activity_main_menu.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:background="#drawable/background"
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.deepseadiver.MainMenu" >
<ImageView
android:id="#+id/Continue"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/title_image" />
<RelativeLayout
android:id="#+id/Button_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:minHeight="150px"
android:minWidth="350px" >
<ImageView
android:id="#+id/Image_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:src="#drawable/button_off" />
<TextView
android:id="#+id/Text_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:text="#string/Start"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</RelativeLayout>
Here is the code for pause_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Rel"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/Continue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/button_off" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="179dp"
android:text="#string/Continue"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="102dp" >
<ImageView
android:id="#+id/Return_Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="#drawable/button_off" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Return_Main"
android:layout_centerVertical="true"
android:layout_marginLeft="178dp"
android:text="#string/Main"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Here is the code for pause.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Pause"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<ImageView
android:id="#+id/Continue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/button_off" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/Pause"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</RelativeLayout>
And here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deepseadiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
>
<activity
android:name="MainMenu"
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="Game"
android:label="#string/title_activity_game">
</activity>
</application>
</manifest>
Sorry I know that's a lot of code but I have spent hours looking at it and cannot find the problem any help is much appreciated.
Log Cat:
04-06 20:44:04.300: E/AndroidRuntime(5657): FATAL EXCEPTION: main
04-06 20:44:04.300: E/AndroidRuntime(5657): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deepseadiver/com.example.deepseadiver.MainMenu}: java.lang.RuntimeException: native typeface cannot be made
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.os.Looper.loop(Looper.java:150)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.main(ActivityThread.java:4277)
04-06 20:44:04.300: E/AndroidRuntime(5657): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 20:44:04.300: E/AndroidRuntime(5657): at java.lang.reflect.Method.invoke(Method.java:507)
04-06 20:44:04.300: E/AndroidRuntime(5657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-06 20:44:04.300: E/AndroidRuntime(5657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-06 20:44:04.300: E/AndroidRuntime(5657): at dalvik.system.NativeStart.main(Native Method)
04-06 20:44:04.300: E/AndroidRuntime(5657): Caused by: java.lang.RuntimeException: native typeface cannot be made
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.graphics.Typeface.<init>(Typeface.java:147)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.graphics.Typeface.createFromAsset(Typeface.java:121)
04-06 20:44:04.300: E/AndroidRuntime(5657): at com.example.deepseadiver.MainMenu.onCreate(MainMenu.java:34)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
04-06 20:44:04.300: E/AndroidRuntime(5657): ... 11 more
You are getting exception from this line-
Typeface Adventure = Typeface.createFromAsset(getAssets(), "Adventure.ttf");
only probable cause is your font path param "Adventure.ttf" is not correct. make sure you have put the ttf file in assets/Adventure.ttf path of your eclipse project.
Related
I'm new to Android developing. This is an assignment which was given to me. I have to convert the value and the converted value must be shown in the next activity. But when I press the submit button its crashes.I don;t know what is the problem. Is there a problem with my RadioButtons. Please help me...
This is the second activity(ConvertActivity)
package com.gihan.temperatureconverter;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
public class ConvertActivity extends ActionBarActivity {
//RadioButton cel=(RadioButton) findViewById(R.id.rCel);
//RadioButton fah=(RadioButton) findViewById(R.id.rFah);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_convert);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_convert, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void calculation(View view) {
EditText val=(EditText) findViewById(R.id.editText);
int value = Integer.valueOf(val.getText().toString()).intValue();
int ans=0;
int fahval=0;
int celval=0;
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.rCel:
if (checked){
ans=((value-32)*5/9);
fahval=value;
celval=ans;}
break;
case R.id.rFah:
if (checked){
ans=((value*9)/5)+32;
celval=value;
fahval=ans;}
break;
}
/*if (cel.isChecked()){
ans=((value-32)*5/9);
fahval=value;
celval=ans;
}
if (fah.isChecked()){
ans=((value*9)/5)+32;
celval=value;
fahval=ans;
}*/
Intent intent = new Intent(ConvertActivity.this, LastActivity.class);
intent.putExtra("celval", getText(celval));
intent.putExtra("fahval", getText(fahval));
ConvertActivity.this.startActivity(intent);
}
}
This is the 2nd XML(activity_convert)
<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="com.gihan.temperatureconverter.ConvertActivity"
android:background="#drawable/ic_background">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp"
android:hint="Enter Value"
android:textSize="20dp"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioGroup"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true">
<RadioButton
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="To Celsius"
android:id="#+id/rCel"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:checked="false"
android:gravity="fill_horizontal"
android:textColor="#ffc80301"
android:textSize="25sp"/>
<RadioButton
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="To Fahrenheit"
android:id="#+id/rFah"
android:checked="false"
android:gravity="fill_horizontal"
android:textColor="#ffc80301"
android:textSize="25sp"
android:layout_below="#+id/rCel"
android:layout_alignLeft="#+id/rCel"
android:layout_alignStart="#+id/rCel" />
</RadioGroup>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/sub"
android:onClick="calculation"
android:layout_below="#+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
</RelativeLayout>
This is the 3rd activity(LastActivity)
package com.gihan.temperatureconverter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class LastActivity extends ActionBarActivity {
TextView vFah=(TextView)findViewById(R.id.vFah);
TextView vCel=(TextView)findViewById(R.id.vCel);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last);//use whatever layout you want.
Bundle extras = getIntent().getExtras();
int celval=extras.getInt("celval");
int fahval=extras.getInt("fahval");
//String cel=String.valueOf(celval);
//String fah=String.valueOf(fahval);
vCel.setText(Integer.toString(celval));
vFah.setText(Integer.toString(fahval));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_convert, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void again(View view) {
Intent intent = new Intent(LastActivity.this, ConvertActivity.class);
LastActivity.this.startActivity(intent);
}
public void home(View view) {
Intent intent = new Intent(LastActivity.this, MainActivity.class);
LastActivity.this.startActivity(intent);
}
}
This is the 3rd XML(activity_last)
<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:background="#drawable/ic_background"
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="com.gihan.temperatureconverter.LastActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Successfully Converted"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textColor="#ffff0004"
android:textSize="30sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:gravity="center_horizontal"
android:textSize="25sp"
android:onClick="home"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Calculate again"
android:id="#+id/button3"
android:layout_marginBottom="45dp"
android:gravity="center_horizontal"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:onClick="again"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Fahrenheit"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textColor="#ff0010ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Celsius"
android:id="#+id/textView4"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:textColor="#ff0010ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="20sp"
android:id="#+id/vFah"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:textColor="#ff9300ff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="20sp"
android:id="#+id/vCel"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:textColor="#ff9300ff"/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gihan.temperatureconverter" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ConvertActivity"
android:label="#string/app_name">
</activity>
<activity
android:name=".LastActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
Logcat
--------- beginning of crash
06-09 21:05:58.413 2381-2381/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.gihan.temperatureconverter, PID: 2381
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.RadioButton
at com.gihan.temperatureconverter.ConvertActivity.calculation(ConvertActivity.java:56)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Some Comments were some ways I tried. It's also failed.
And also this is Android Studio Project.
Your Error Solved Check this code
mainactivity.java
public class MainActivity extends ActionBarActivity {
EditText val;
public static int ans;
public static String fahval="";
public static String celval="";
RadioButton Box1,Box2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
val =(EditText) findViewById(R.id.editText);
}
public void calculation(View v)
{
//get value from edit text box and convert into double
double a=Double.parseDouble(String.valueOf(val.getText()));
Box1 = (RadioButton) findViewById(R.id.rCel);
Box2 = (RadioButton) findViewById(R.id.rFah);
//check which radio button is checked
if(Box1.isChecked())
{
//display conversion
celval = String.valueOf(f2c(a));
// Toast.makeText(getApplicationContext(), celval,Toast.LENGTH_LONG).show();
Box1.setChecked(true);
}
else
{
fahval = String.valueOf(c2f(a));
// Toast.makeText(getApplicationContext(), fahval,Toast.LENGTH_LONG).show();
Box2.setChecked(true);
}
Intent intent = new Intent(this, LastActivity.class);
intent.putExtra("celval", celval);
intent.putExtra("fahval", fahval);
startActivity(intent);
}
//Celcius to Fahrenhiet method
private double c2f(double c)
{
return (c*9)/5+32;
}
//Fahrenhiet to Celcius method
private double f2c(double f)
{
return (f-32)*5/9;
}
}
LastActivity.java
public class LastActivity extends ActionBarActivity {
TextView vFah;
TextView vCel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.last);//use whatever layout you want.
vFah=(TextView)findViewById(R.id.vFah);
vCel=(TextView)findViewById(R.id.vCel);
Bundle extras = getIntent().getExtras();
String celval=extras.getString("celval");
String fahval=extras.getString("fahval");
//String cel=String.valueOf(celval);
//String fah=String.valueOf(fahval);
vCel.setText(celval);
vFah.setText(fahval);
}
public void again(View view) {
Intent intent = new Intent(LastActivity.this, MainActivity.class);
LastActivity.this.startActivity(intent);
}
public void home(View view) {
Intent intent = new Intent(LastActivity.this, MainActivity.class);
LastActivity.this.startActivity(intent);
}
}
I was building some apps this week, and couldn't figure out why I couldn't navigate from the Main Activity to a second activity and from there to a 3rd level activity (that would pop to 2nd activity), and app kept crashing when I added the 3rd. Finally looked in the Logcat to figure out what's wrong, and then read "Cannot Navigate from 'second activity'.... This View will not accept Navigation". Searched through code to find errors, but Never thought to look there before.
i'm making a kind of a form app for my end work for school,
but as you can see in the picture, it ain't going verry well.
I'm trying to save all the date from the textviews into a toast message so i can later on put it into a text file.
But now the Toast file says that all my data is "false"
please help me..
http://prntscr.com/7b4how
package com.jan.energyservice;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Nieuw_huis1 extends ActionBarActivity {
EditText tekst;
String s = "jantest";//AlgemeneGegevens.getFilename();
String uitvoer;
private String data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nieuw_huis1);
setupVolgendePagina();
}
private void SlaOp(String String, String String2, String String3, String String4, String String5,
String String6, String String7, String String8, int rdbgrouphuisstatus, String String10,
String String11, String String12, String String13, String String14, String String15,
String String16, String String17, String Titel) {
try{
uitvoer += Titel + "\n";
save(String,String10);
save(String2,String11);
save(String3,String12);
save(String4,String13);
save(String6,String15);
save(String7,String16);
save(String8,String17);
save(rdbgrouphuisstatus);
toonresultaat();
}
catch (Exception e) {
}
}
private void toonresultaat() {
// TODO Auto-generated method stub
Toast.makeText(this, uitvoer,Toast.LENGTH_LONG ).show();
}
private void save(int rdbgrouphuisstatus) {
// TODO Auto-generated method stub
}
private void save(String invoerTekst, String label) {
try {
uitvoer += label + "\n" + invoerTekst;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void setupVolgendePagina() {
Button Movebutton = (Button) findViewById(R.id.btnVolgendePagina1);
// 2 click listener to run code
Movebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent =(new Intent(Nieuw_huis1.this,Nieuw_huis2.class));
SlaOp(getText(R.id.txtProjectNaam).toString(),getText(R.id.txtKenmerk).toString(),getText(R.id.txtAdres).toString(),
getText(R.id.txtGemeente).toString(),getText(R.id.txtKlantNaam).toString(),getText(R.id.txtDatumBezoek).toString(),
getText(R.id.txtNaamAdviseur).toString(),getText(R.id.txtHuisnummer).toString(),R.id.rdbgroupHuisStatus,
getText(R.id.tvProjectnaam).toString(),getText(R.id.tvKenmerk).toString(),getText(R.id.tvAdres).toString(),
getText(R.id.tvGemeente).toString(),getText(R.id.tvKlantnaam).toString(),getText(R.id.tvDatumBezoek).toString(),
getText(R.id.tvNaamAdviseur).toString(),getText(R.id.tvHuisnummer).toString(),getText(R.id.tvAdresgegevens).toString());
startActivity(intent);
}
});
}
public void save(View view){
//data = tekst;
try {
FileOutputStream fOut = openFileOutput(s,Context.MODE_PRIVATE);
new PrintStream(fOut).println("Naam van eerste ding");
fOut.write(data.getBytes());
fOut.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nieuw_huis1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is my xml file
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/TableLayout1"
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.jan.energyservice.Nieuw_huis1" >
<TextView
android:id="#+id/tvAdresgegevens"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/adresgegevens"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvProjectnaam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/projectnaam"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtProjectNaam"
android:layout_width="340dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/tvKenmerk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/kenmerk"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtKenmerk"
android:layout_width="365dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
<TextView
android:id="#+id/tvAdres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/adres"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtAdres"
android:layout_width="370dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text|textPersonName" />
<TextView
android:id="#+id/tvGemeente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gemeente"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtGemeente"
android:layout_width="356dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date|text" />
<TextView
android:id="#+id/tvKlantnaam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/klantnaam"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtKlantNaam"
android:layout_width="356dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="#+id/tvDatumBezoek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/datumbezoek"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtDatumBezoek"
android:layout_width="357dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
<TextView
android:id="#+id/tvNaamAdviseur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/naamadviseur"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtNaamAdviseur"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="#+id/tvHuisnummer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Huisnummer" />
<EditText
android:id="#+id/txtHuisnummer"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<RadioGroup
android:id="#+id/rdbgroupHuisStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rdbVerhuur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/Verhuur" />
<RadioButton
android:id="#+id/rdbVerkoop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/Verkoop" />
<RadioButton
android:id="#+id/rdbImmo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/Immo" />
</RadioGroup>
<Button
android:id="#+id/btnVolgendePagina1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/VolgendePagina" />
</TableLayout>
this is what i edited after your responses.. but still not working
public void onClick(View v) {
Intent intent =(new Intent(Nieuw_huis1.this,Nieuw_huis2.class));
EditText txtProjectnaam = (EditText) findViewById(R.id.txtProjectNaam);
EditText txtKenmerk = (EditText) findViewById(R.id.txtKenmerk);
EditText txtAdres = (EditText) findViewById(R.id.txtAdres);
EditText txtNaamAdviseur = (EditText) findViewById(R.id.txtNaamAdviseur);
EditText txtHuisnummer = (EditText) findViewById(R.id.txtHuisnummer);
EditText txtGemeente = (EditText) findViewById(R.id.txtGemeente);
EditText txtKlantnaam = (EditText) findViewById(R.id.txtKlantNaam);
EditText txtDatumBezoek = (EditText) findViewById(R.id.txtDatumBezoek);
TextView tvProjectnaam = (TextView) findViewById(com.jan.energyservice.R.string.projectnaam);
TextView tvKenmerk = (TextView) findViewById(com.jan.energyservice.R.string.kenmerk);
TextView tvAdres = (TextView) findViewById(com.jan.energyservice.R.string.projectnaam);
TextView tvGemeente = (TextView) findViewById(com.jan.energyservice.R.string.gemeente);
TextView tvKlantnaam = (TextView) findViewById(com.jan.energyservice.R.string.klantnaam);
TextView tvDatumBezoek = (TextView) findViewById(com.jan.energyservice.R.string.datumbezoek);
TextView tvNaamAdviseur = (TextView) findViewById(com.jan.energyservice.R.string.naamadviseur);
TextView tvHuisnummer = (TextView) findViewById(com.jan.energyservice.R.string.Huisnummer);
TextView tvAdresGegevens = (TextView) findViewById(com.jan.energyservice.R.string.adresgegevens);
SlaOp(txtProjectnaam.getText().toString(),txtKenmerk.getText().toString(),txtAdres.getText().toString(),
txtGemeente.getText().toString(),txtKlantnaam.getText().toString(),txtDatumBezoek.getText().toString(),
txtNaamAdviseur.getText().toString(),txtHuisnummer.getText().toString(),R.id.rdbgroupHuisStatus,
tvProjectnaam.getText().toString(),tvKenmerk.getText().toString(),tvAdres.getText().toString(),
tvGemeente.getText().toString(),tvKlantnaam.getText().toString(), tvDatumBezoek.getText().toString(),
tvNaamAdviseur.getText().toString(),tvHuisnummer.getText().toString(),
tvAdresGegevens.getText().toString());
startActivity(intent);
}
});
05-30 19:26:34.383: E/AndroidRuntime(898): FATAL EXCEPTION: main
05-30 19:26:34.383: E/AndroidRuntime(898): java.lang.NullPointerException
05-30 19:26:34.383: E/AndroidRuntime(898): at com.jan.energyservice.Nieuw_huis1$1.onClick(Nieuw_huis1.java:115)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.view.View.performClick(View.java:4204)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.view.View$PerformClick.run(View.java:17355)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.os.Handler.handleCallback(Handler.java:725)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.os.Looper.loop(Looper.java:137)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 19:26:34.383: E/AndroidRuntime(898): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 19:26:34.383: E/AndroidRuntime(898): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 19:26:34.383: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 19:26:34.383: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 19:26:34.383: E/AndroidRuntime(898): at dalvik.system.NativeStart.main(Native Method)
05-30 19:26:37.943: E/Trace(3262): error opening trace file: No such file or directory (2)
It says something about line 115 which is in SlaOp from the moment i start with the textviews
Inject your view before you use it.
Do this with all view you wanna to use/modify:
EditText txtKenmerk = (EditText) findViewById(R.id.txtKenmerk)
Then:
txtKenmerk.getText().toString();
I check your latest code and you have a problem with your onClick method starting from your TextView's allocations.
It's an error to cast findViewById(com.jan.energyservice.R.string.projectnaam)
to a TextView since it's not a TextView.
What you should really do is (TextView) findViewById(R.id.projectnaam). that what you configured in your activity xml code. (use R.id for all your objects)
R.string is a special location (string.sml) were you can save all your strings.
The null exception that you got is because you tried to get a view that doesn't exist and therefore you textview was null. afterwards you tried to read from a null function and the exception popped.
Error(s):
10-17 18:53:39.298: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quizme/com.quizme.QuizMeActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView
10-17 18:53:38.747: error opening trace file: No such file or directory (2)
Upon trying to run it I get the unfortunatly, the app does not work error as well.
Java file:
package com.quizme;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class QuizMeActivity extends Activity {
private TextView question;
private TextView result;
private ImageButton forward;
private ImageButton backward;
private Button bTrue;
private Button bFalse;
private TrueFalse mTrueFalse;
/*
* onCreate(Bundle savedInstanceState)
* Instantiates all private GUI elements to their corresponding Views
* in activity_quiz_me.xml
* Sets the string values of the questions in the TrueFalse object,
* and sets their truth values.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_me);
Resources res = this.getResources(); //this is to be able to get strings
bTrue=(Button)findViewById(R.id.true_button);
forward=(ImageButton)findViewById(R.id.forward_button);
bFalse=(Button)findViewById(R.id.false_button);
backward=(ImageButton)findViewById(R.id.back_button);
result=(TextView)findViewById(R.id.result);
result.setText("");
question=(TextView)findViewById(R.id.question);
question.setText(mTrueFalse.getmQuestionSet()[0]);
mTrueFalse.setmQuestionSet(0, (String) res.getText(R.string.q1));
mTrueFalse.setmQuestionSet(1, (String) res.getText(R.string.q2));
mTrueFalse.setmQuestionSet(2, (String) res.getText(R.string.q3));
mTrueFalse.setmQuestionSet(3, (String) res.getText(R.string.q4));
mTrueFalse.setmQuestionSet(4, (String) res.getText(R.string.q5));
mTrueFalse.setmArrOfBols(0, false);
mTrueFalse.setmArrOfBols(1, true);
mTrueFalse.setmArrOfBols(2, true);
mTrueFalse.setmArrOfBols(3, false);
mTrueFalse.setmArrOfBols(4, true);
}
public void checkTrue(View view) {
Resources res = this.getResources(); //this is to be able to get strings
if(mTrueFalse.getCurrentBolValue()==true)
{
result.setText((String) res.getText(R.string.right));
}
else
{
result.setText((String) res.getText(R.string.wrong));
}
}
public void checkFalse(View view) {
Resources res = this.getResources(); //this is to be able to get strings
if(mTrueFalse.getCurrentBolValue()==true)
{
result.setText((String) res.getText(R.string.wrong));
}
else
{
result.setText((String) res.getText(R.string.right));
}
}
public void goForward(View view) {
if(mTrueFalse.getmCurrentQ()<mTrueFalse.getmQuestionSet().length-1)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmCurrentQ()+1);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
else if(mTrueFalse.getmCurrentQ()==mTrueFalse.getmQuestionSet().length-1)
{
mTrueFalse.setmCurrentQ(0);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
}
public void goBack(View view) {
if(mTrueFalse.getmCurrentQ()>0)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmCurrentQ()-1);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
else if(mTrueFalse.getmCurrentQ()==0)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmQuestionSet().length-1); //for the purposes of the project, its setting currentQ to 4
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz_me, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout xml file:
<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:orientation="vertical"
android:id="#+id/activity_view"
tools:context="com.quizme.QuizMeActivity">
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sTrue"
android:layout_marginRight="180dp"
android:onClick="checkTrue"
/>
<ImageButton
android:id="#+id/forward_button"
android:contentDescription="#string/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/back_button"
android:onClick="goForward"
android:src="#drawable/forward" />
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/question"
android:layout_toRightOf="#+id/true_button"
android:onClick="checkFalse"
android:text="#string/sFalse" />
<ImageButton
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="70dp"
android:contentDescription="#string/back"
android:onClick="goBack"
android:src="#drawable/back" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/resultStr"
android:textSize="30sp" />
<TextView
android:id="#+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/true_button"
android:layout_marginTop="34dp"
android:layout_toLeftOf="#+id/forward_button"
android:text="#string/questionStr"
android:textSize="25sp" />
</RelativeLayout>
Android Manifest:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".QuizMeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
sorry for the pile of code, I am just frustrated and stuck
Try cleaning the project
Eclipse: Project->Clean
When I ran the app the first activity worked but as soon as it went to the second activity it said treasure hunt has stopped working, and there was nothing under logcat or console. I am pretty sure it is a problem with level_1.java but can't figure out what. Please help thanks.
Main Activity:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void Start(View view) {
Log.d("hi", "hello");
Intent intent = new Intent(this, Level_1.class);
startActivity(intent);
finish();
}
}
Mainxml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Treasure Hunt" />
<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="82dp"
android:text="Start"
android:onClick="Start" />
</RelativeLayout>
`
level_1java:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
public class Level_1 extends Activity {
private SharedPreferences settings;
private SharedPreferences.Editor editor;
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
String hint;
String answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.level_1, menu);
return true;
}
public void level1() {
editor.putInt("level", 1); // only needs to be done for level 1
editor.commit();
Clue.setText("Bartholdi was my creator, I carry fire and knowledge, what am I?");
hint = "July IV MDCCLXXVI";
answer = "statue of liberty";
}
public void answer() {
String useranswer = edittext.toString();
if (useranswer.equalsIgnoreCase(answer)) {// if they get the correct answer
int leveltest = settings.getInt("level", 1);
editor.putInt("level", leveltest+1); //adds one level to the current level
editor.commit();
}
}
public void hint() { //function that displays the hint
}
}
level_1 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=".Level_1" >
<TextView
android:id="#+id/Clue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Clue" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Clue"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/editText"
android:layout_marginBottom="50dp"
android:text="Submit"
android:onClick="answer"/>
<Button
android:id="#+id/Hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Submit"
android:layout_alignBottom="#+id/Submit"
android:layout_alignLeft="#+id/editText"
android:text="Hint"
android:onClick="Hint"/>
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.treasurehunt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.treasurehunt.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.treasurehunt.Level_1"
android:label="#string/title_activity_level_1" >
</activity>
</application>
</manifest>
Seems you're having NullPointerException.
On the field declaration
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
Move the assignments to in onCreate()
public class Level_1 extends Activity
{
// Other members ....
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
// Assignemnts should be here
Clue = (TextView)findViewById(R.id.Clue);
edittext = (EditText)findViewById(R.id.editText);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
break;
}
}
}
I am facing an issue with a button onclick. I checked with previously asked questions, but was not able to figure out the problem, where exactly it lies. The first time, when I click on, 'Next' button, it successfully moves to next page, but in the second page, after entering details, when the 'Proceed'button is clicked, the application crashes.
Activity1:
package com.application.P1;
//import com.application.P1.R;
//import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class P1Activity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Using TextView to Give a Home Page Screen
TextView tv=new TextView(this);
tv.setText("WELCOME TO Application Click on Next to Proceed");
setContentView(R.layout.main);
}
public void Welcome( View v){
// Toast.makeText(this, "Please enter your Nickname and proceed further", Toast.LENGTH_SHORT).show();
Intent myActivity = new Intent(this,Activity2.class);
startActivity(myActivity);
// EditText t
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Activity2:
package com.application.P1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Activity2 extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
public void Proceed4( View v){
// Toast.makeText(this, "Thanks for entering your nickname", Toast.LENGTH_SHORT).show();
Intent myActivity2 = new Intent(this,Activity3.class);
startActivity(myActivity2);
// EditText t
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Activity3:
package com.application.P1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
//import android.content.Intent;
//import android.widget.Button;
import android.widget.Toast;
public class Activity3 extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
All the three activities are in same package. In the second activity, after button click, when a message is toasted, the toasted message is coming properly. But, when the next activity is called, it is not coming.
XML Coding:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/backrepeat"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:text="#string/Next"
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="Welcome"/>
</LinearLayout>
login.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/mynickname" />
<EditText
android:id="#+id/mynickname3"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="#string/Proceed"
android:id="#+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="Proceed4"/>
</LinearLayout>
Check.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello3" />
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.application.P1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/cherry"
android:label="#string/app_name" >
<activity
android:name=".P1Activity"
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=".Activity2">
</activity>
<activity android:name=".Activity3"></activity>
</application>
</manifest>
03-22 00:50:27.125: E/AndroidRuntime(857): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 00:50:27.125: E/AndroidRuntime(857): at java.lang.reflect.Method.invoke(Method.java:507)
03-22 00:50:27.125: E/AndroidRuntime(857): at android.view.View$1.onClick(View.java:2139)
03-22 00:50:27.125: E/AndroidRuntime(857): ... 11 more
03-22 00:50:27.125: E/AndroidRuntime(857): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity
class {com.application.P1/com.application.P1.Activity3}; have you declared this activity in your AndroidManifest.xml?
03-22 00:50:27.125: E/AndroidRuntime(857): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
03-22 00:50:27.125: E/AndroidRuntime(857): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
03-22 00:50:27.125: E/AndroidRuntime(857): at android.app.Activity.startActivityForResult(Activity.java:2827)
03-22 00:50:27.125: E/AndroidRuntime(857): at android.app.Activity.startActivity(Activity.java:2933)
03-22 00:50:27.125: E/AndroidRuntime(857): at com.application.P1.Activity2.Proceed4(Activity2.java:31)
03-22 00:50:27.125: E/AndroidRuntime(857): ... 14 more
03-22 00:50:29.867: I/Process(857): Sending signal. PID: 857 SIG: 9
Values.xml:
<string name="hello">Welcome to Expresso Application</string>
<string name="Next">Next</string>
<string name="app_name">Expresso2</string>
<string name="Proceed">Proceed</string>
<string name="Nickname">Nickname</string>
<string name="mynickname">myNickname</string>
<string name="hello3">Tensions</string>
Based on the stack trace, it appears that your manifest file does not declare the activity. Check that there is an <activity> tag for Activity3.
fist replace in Activity A:
this
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Using TextView to Give a Home Page Screen
TextView tv=new TextView(this);
tv.setText("WELCOME TO Application Click on Next to Proceed");
setContentView(R.layout.main);
}
from this code :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Using TextView to Give a Home Page Screen
TextView tv=new TextView(this);
tv.setText("WELCOME TO Application Click on Next to Proceed");
}