Clicking the button to second activity automatically closes app [Android Studio] - java

Background
Just to set some context, I'm very new to Android app development. After using Android Developers guide on developing a basic app that would teach me how to create a "Hello, World!" project. However, after learning to build a user interface and start another activity. Ultimately, the main focus was to add some code to the MainActivity that starts a new activity to display a message when the user would tap the Send button. After running the app on my device, I attempted to type out a message and click send, only for the send button to ultimately close my app with no lag, or explaination as to why it closed.
MainActivity.java
package com.example.giglitz;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.giglitz">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".DisplayMessageActivity">
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
DisplayMessageActivity.java
package com.example.giglitz;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayMessageActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Logcat (after filtering Error, Show only selected application)
Process: com.example.giglitz, PID: 6422
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:7201)
at android.view.View.performClickInternal(View.java:7170)
at android.view.View.access$3500(View.java:806)
at android.view.View$PerformClick.run(View.java:27562)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:7201) 
at android.view.View.performClickInternal(View.java:7170) 
at android.view.View.access$3500(View.java:806) 
at android.view.View$PerformClick.run(View.java:27562) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.giglitz.MainActivity.sendMessage(MainActivity.java:24)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
at android.view.View.performClick(View.java:7201) 
at android.view.View.performClickInternal(View.java:7170) 
at android.view.View.access$3500(View.java:806) 
at android.view.View$PerformClick.run(View.java:27562) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>

I think you either did not put anything in the 'onclick' field for your button, or you didn't set an ID for your EditText. Can you send your activity_main.xml file as well? If you want to know where it is, go to the 'res' directory, and then go into the 'layout' directory.

I will need to see your logcat output to determine what problems you encountered. Generally, when using Intent extras, I recommend checking for nullability of objects, as that string might not exist, although it clearly does in this case. So, please send a transcript of your logs.
Thanks!

It looks like you did not implement the 'onclick' behavior correctly in your app. In the XML file, you should add this line for your send button android:onClick="sendMessage"
Also, check out this for more details about implementing onClick behaviors in Android: https://developer.android.com/guide/topics/ui/controls/button#HandlingEvents, https://stackoverflow.com/a/4153842/1725535

It looks like you did not implement the 'onclick' behavior correctly in your app. In the XML file, you should add this line for your send button android:onClick="sendMessage"
To tell activity to listen the button click and call the onClickListner method.

It looks like you did not implement the 'onclick' behavior correctly in your app.
In the XML file, you should add this line for your send button
android:onClick="sendMessage" or you can use it programmatically by button id
For eg:-
Button btn = (Button) findViewById(R.Id.sendButton);
btn.setOnClickListner(new View.OnClickListner){
#Override
public onClick(View view){
// Your statement
}
}
To tell activity to listen the button click and call the onClickListner method.

Related

Creating a simple app, can't get clear button to work

So, I'm trying to follow along this website's tutorial.
https://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent
I did the first part where I got the text box and a send button. I can enter text in the text box and click send. But the text box's text stays there. It doesn't disappear. So, the next part of the tutorial is how to get the text to disappear. I'm trying to follow along, but I can't get it to work.
Here's my code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/editText"
android:layout_width="183dp"
android:layout_height="46dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button_clear"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="#string/button_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText"
tools:layout_editor_absoluteY="14dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
<resources>
<string name="app_name">The Most Useless App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
</resources>
MainActivity.xml
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
MainActivity.java
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
Errors I get
Cannot resolve symbol textView
Cannot resolve symbol activity_display_message
Cannot resolve symbol EXTRA_MESSAGE
The activity com.example.myfirstapp.MainActivity is not registered in
the manifest.
Any help would be appreciated.
Is a setting problem, the first thing you should do is
to do a clean build, you can select above android studio the option buld and then clean build. If does not work please post the output of your build.gradle file
There is also the possibility that you are just starting with android, and you do not know that have to import sometimes the needed libraries
Point your mouse to MainActivity and press alt+enter if you are in windows, and select add Activity to AndroidManifest, then do a clean build.
I suggest you to use the wizard building a new project, you go to Android studio and select file new project and follow the project, so MainActivity will be automatically added to your manifest

Android app always show app name in toolbar

I want to show a specific text in my app toolbar but it also always shows the name of the app too. How can I get rid of the app name and only the text of the TextView?
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="true" >
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:fitsSystemWindows="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="The title I want to show"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Add label to activity declaration in AndroidManifest.xml file.
<activity
....
android:label="Name of Your Screen"
....
</activity>
For static activity name, set android:label="Activity Name" attribute for each activity in AndroidManifest.xml
...
<activity
...
android:label="Activity Name">
...
</activity>
...
For dynamic activity name, you can use the following:
getActionBar().setTitle("Activity Name");
To provide compatibility across all the android versions, use:
getSupportActionBar().setTitle("Activity Name");
But if you want to completely get rid of the title bar, you can extend Activity instead of AppCompatActivity or you can hide the action bar:
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
you just write this line in your activity getSupportActionBar().setDisplayShowTitleEnabled(false);
and it will set the visibility of the app name to invisible
Use
<activity
android:label="blah-blah" />
to set it in the manifest file.
Or in the code toolbar.setTitle("blah-blah");
If you set your Toolbar like ActionBar then you can call actionBar.setTitle("blah-blah");.
From the modification of the accepted answer, this actually resolved my issue.
All you need to do is to leave the "label name" text as blank in your manifest as example below
...
<activity
...
android:label=" ">
...
</activity>
...
this will actually enable only your textview text in your toolbar widget to show, it works very fine for me

Getting error :- unfortunately app has stopped in android

I have been searching for this for hours but all in vain.I am developing a tutorial app named Yamba from the book learning android .It is an app to update status on twitter.It uses jtwitter.jar library. Things which I am using :
linux(ubuntu)
ant debug ( to build my project)
my android phone for testing the app.
The project builds successfully and installs the app on my phone but when I run the app, I get the error "unfortunately, Yamba has stopped".I have the following files:
AndroidManifest.xml file as--
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Yamba"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name=".Yamba"
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>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
my Yamba.java file as --
package com.example.Yamba;
import winterwell.jtwitter.Twitter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Button;
public class Yamba extends Activity implements OnClickListener
{
private static final String TAG = "Yamba";
EditText editText;
Button updateButton;
Twitter twitter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
//find views
editText =(EditText) findViewById(R.id.editText);
updateButton =(Button) findViewById(R.id.buttonUpdate);
updateButton.setOnClickListener(this);
twitter = new Twitter("student","password");
twitter.setAPIRootUrl("http://yamba.marakana.com/api");
}
public void onClick(View view){
twitter.setStatus(editText.getText().toString());
Log.d(TAG,"onClicked");
}
}
my layout.xml as ----
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/titleStatus"
android:textSize="30sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:hint="#string/hintText"
android:id="#+id/editText"
android:gravity="top|center_horizontal">
</EditText>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/buttonUpdate"
android:textSize="20sp"
android:id="#+id/buttonUpdate">
</Button>
</LinearLayout>
my logcat is as follows ---
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
E/ObjectHelper( 6376): Can't find method:setCompatibilityInfo
E/Trace ( 6342): error opening trace file: No such file or directory (2)
E/jdwp ( 6342): jdwp::setsockopt(SO_SNDTIMEO)
E/jdwp ( 6342): jdwp::setsockopt(SO_RCVTIMEO)
E/KeyguardUpdateMonitor( 508): Object tried to add another callback
E/KeyguardUpdateMonitor( 508): java.lang.Exception: Called by
E/KeyguardUpdateMonitor( 508): at com.android.internal.policy.impl.keyguard.KeyguardUpdateMonitor.registerCallback(KeyguardUpdateMonitor.java:1107)
E/KeyguardUpdateMonitor( 508): at com.android.internal.policy.impl.keyguard.KeyguardSelectorView.onResume(KeyguardSelectorView.java:331)
E/KeyguardUpdateMonitor( 508): at com.android.internal.policy.impl.keyguard.KeyguardHostView.onScreenTurnedOn(KeyguardHostView.java:1118)
E/KeyguardUpdateMonitor( 508): at com.android.internal.policy.impl.keyguard.KeyguardViewManager.onScreenTurnedOn(KeyguardViewManager.java:404)
E/KeyguardUpdateMonitor( 508): at com.android.internal.policy.impl.keyguard.KeyguardViewMediator.handleNotifyScreenOn(KeyguardViewMediator.java:1775)
E/KeyguardUpdateMonitor( 508): at com.android.internal.policy.impl.keyguard.KeyguardViewMediator.access$2300(KeyguardViewMediator.java:106)
E/KeyguardUpdateMonitor( 508): at com.android.internal.policy.impl.keyguard.KeyguardViewMediator$4.handleMessage(KeyguardViewMediator.java:1413)
E/KeyguardUpdateMonitor( 508): at android.os.Handler.dispatchMessage(Handler.java:107)
E/KeyguardUpdateMonitor( 508): at android.os.Looper.loop(Looper.java:194)
E/KeyguardUpdateMonitor( 508): at android.os.HandlerThread.run(HandlerThread.java:60)
E/AudioMTKHardware( 137): setParameters() still have param.size() = 1, remain param = "screen_state=on"
E/CellLocation( 784): create GsmCellLocation
E/CellLocation( 784): create GsmCellLocation
E/AndroidRuntime( 6411): FATAL EXCEPTION: main
E/AppErrorDialog( 508): Failed to get ILowStorageHandle instance
Clueless about what to do now.
Any help in this regard would be deeply appreciated.
EDIT-- I have posted my new logcat after removing the typo.
<uses-permission android:name="android.persmission.INTERNET"/>
Wrongly named permission... It should be:
<uses-permission android:name="android.permission.INTERNET"/>
there is a typo error in android.persmission.INTERNET

UPDATED: Android: java.lang.RuntimeException: Unable to start activity ComponentInfo

UPDATE** so i did pretty much everything that was said below and still got this error(see new logcat file). Its different though so i dont know how to fix it.**
Ok so im a beginner so i really dont know whats going on. When i try to run this app in the emulator all it is "unfortunately this app has stopped." Ive search all the question similar to this but haven't been able to find an answer. btw this isnt a finished app or really even started, but i want to get it to run before i continue. Heres the log cat file thing.
error opening trace file: No such file or directory (2)
08-19 06:09:04.579: D/AndroidRuntime(1006): Shutting down VM
08-19 06:09:04.599: W/dalvikvm(1006): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2103)
at android.content.res.Resources.getLayout(Resources.java:852)
at android.view.MenuInflater.inflate(MenuInflater.java:107)
at com.nickymilton.testbasebal3.MainActivity.onCreateOptionsMenu(MainActivity.java:22)
at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:393)
at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2913)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Now heres the activty_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="#string/EnterHome"
android:textColor="#color/White" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="#string/home_team"
android:layout_below="#id/textView1"
android:shadowColor="#color/ICSBlue" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="#string/EnterAway"
android:textColor="#color/White" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="#string/away_team"
android:shadowColor="#color/ICSBlue" />
<Button
android:id="#+id/button3"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_below="#id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="#string/Enter" />
<Button
android:id="#+id/button2"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#id/button3"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="#string/Skip" />
</RelativeLayout>
Now the activity_display_message.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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".DisplayMessageActivity" />
</RelativeLayout>
Then here the mainActivity.java file
package com.nickymilton.testbasebal3;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.nickymilton.Testbasebal3.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText1);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
then the second activity DisplayMessageActivity.java
package com.nickymilton.testbasebal3;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class DisplayMessageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
}
And finally the mainfest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nickymilton.testbasebal3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="#string/title_activity_display_message" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.nickymilton.testbasebal3.MainActivity" />
</activity>
</application>
</manifest>
So there it is. Someone please help thanks!
There are issues with your XML - you can tell from the line:
Binary XML file line #54: Error inflating class <unknown>
One thing I spotted is that when you declare an android:id attribute, it needs to have a + in it, like this:
android:id="#+id/location"
This is because you're creating an id, not just referring to a pre-existing one defined elsewhere (like you might be with a string, e.g "#string/hello" might be in the values folder).
I've noticed one thing, you've not declared the 'id' values correctly in some of your xml.
android:id="#id/button3"
should be
android:id="#+id/button3"
And like wise for your other id declarations.
You'll notice that in your stack trace it gives you the line number in your xml for the location of the error.
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.nickymilton.testbasebal3/com.nickymilton.testbasebal3.MainActivity}:
android.view.InflateException: Binary XML file line #54: Error inflating class <unknown>
This might be something else. But essentially you have badly formed xml.
Error Two
You need to look through the stack trace backwards to find the source of your error, you will then most likely find the solution yourself.
android.content.res.Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2103)
at android.content.res.Resources.getLayout(Resources.java:852)
at android.view.MenuInflater.inflate(MenuInflater.java:107)
at com.nickymilton.testbasebal3.MainActivity.onCreateOptionsMenu(MainActivity.java:22)
at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
From the top down you'll get to the line MainActivity.oncreateOptionsMenu()
Above that you'll see that its trying to inflate (your options menu xml), i.e. loadXmlResourceParser()
It doesn't go as far as giving you an xml line number but it does tell you you have an issue with an Resource id not being valid.
You'll need to post your options xml in your question if you want an answer. But i would see if you can work it out your self first. Try commenting out elements and putting them back in to see whats broken. I say this because theres only so much one Stack overflow question should answer.
In the declaration for one of your TextViews you have :
android:textSize="#string/TextSizefifteen"
textSize should be a number or a dimension like:
android:textSize="#dimen/TextSizefifteen"
See what you have at TextSizefifteen and how you declared it(see http://developer.android.com/guide/topics/resources/more-resources.html#Dimension).
For example:
<dimen name="TextSizefifteen">25dp</dimen>

Android TabLayout crashing

I am creating an android app myself (min sdk version 7 or 2.1 and up). And it was going fine until I decided I was going to need a TabLayout. I added a new class, and had it extend TabActivity, however when launching the app in an AVD it crashed and stated this in the log that there was an uncaught exception. And that it could not launch the activity.
ThermostatTabHost.java (note, I did not add package/import to save some space):
public class ThermostatTabHost extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmain);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ThermostatActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Temperature").setIndicator("Program",
res.getDrawable(R.drawable.arrow_up))
.setContent(intent);
tabHost.addTab(spec);
}
}
The ThermostatActvity class currently only holds a onCreate function which calls super.onCreate(savedInstance) and sets a layout with a textview.
The tabmain.xml file:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
And finally the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pack.thermostat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".ThermostatTabHost"
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>
This example came straight of the android developers website (with a few minor changes), I also looked around for similar issues but I couldn't come up with anything.
Thank you in advance.
Add this "ThermostatActivity" in your manifeast file

Categories