I'm having trouble opening an activity. In my first activity (activity_main), I have 3 buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFF"
android:id="#+id/root"
>
<TableRow android:layout_margin="0dip"
android:id="#+id/row1">
<Button android:id="#+id/button01"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="#string/buttonText1"
android:layout_marginTop="60dip"
android:layout_marginBottom="15dip"
android:padding="15dip" />
</TableRow>
<TableRow android:layout_margin="0dip"
android:id="#+id/row2">
<Button android:id="#+id/button02"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="#string/buttonText2"
android:padding="15dip" />
</TableRow>
<TableRow android:layout_margin="0dip"
android:id="#+id/row3">
<Button android:id="#+id/button03"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_marginTop="15dip"
android:hint="#string/buttonText3"
android:onClick="browseExercises"
android:padding="15dip" />
</TableRow>
</TableLayout>
</LinearLayout>
I click on the 3rd button, and my second activity opens, which consists of two additional buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFF"
android:id="#+id/root"
>
<TableRow android:layout_margin="0dip"
android:id="#+id/row1">
<Button android:id="#+id/button01"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="#string/alphabetical"
android:layout_marginTop="60dip"
android:layout_marginBottom="15dip"
android:onClick="Alphabetical"
android:padding="15dip" />
</TableRow>
<TableRow android:layout_margin="0dip"
android:id="#+id/row2">
<Button android:id="#+id/button99999"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="#string/group"
android:onClick="muscleGroups"
android:padding="15dip" />
</TableRow>
</TableLayout>
</LinearLayout>
I click on either of these buttons, and I get an error telling me that "Unfortunately, Test_Project has stopped."
I already have an activity created for each of these two buttons, and everything for these two buttons are coded the exact same way as when I created my second activity, which opens just fine, and has buttons I can click on without causing any errors. I think I'm missing something fundamental here, it's like if I create too many activities, they aren't able to look back at my MainActivity.java file for the method that is supposed to open the next activity. Here is my MainActivity.java code:
package com.example.test_project;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void browseExercises(View view){
Intent intent = new Intent(this, BrowseExercises.class);
startActivity(intent);
}
public void Alphabetical(View view){
Intent intent = new Intent(this, Alphabetical.class);
startActivity(intent);
}
public void muscleGroups(View view){
Intent intent = new Intent(this, MuscleGroups.class);
startActivity(intent);
}
#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;
}
}
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.test_project"
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.test_project.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.test_project.BrowseExercises"
android:label="#string/title_activity_browse_exercises"
>
</activity>
<activity
android:name="com.example.test_project.MuscleGroups"
android:label="#string/title_activity_muscle_groups" >
</activity>
<activity
android:name="com.example.test_project.Alphabetical"
android:label="#string/title_activity_alphabetical" >
</activity>
</application>
</manifest>
And my logcat file:
04-20 13:46:57.632: W/dalvikvm(26747): threadid=1: thread exiting with uncaught exception (group=0x40dc42a0)
04-20 13:46:57.639: E/AndroidRuntime(26747): FATAL EXCEPTION: main
04-20 13:46:57.639: E/AndroidRuntime(26747): java.lang.IllegalStateException: Could not find a method muscleGroups(View) in the activity class com.example.test_project.BrowseExercises for onClick handler on view class android.widget.Button with id 'button99999'
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.view.View$1.onClick(View.java:3587)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.view.View.performClick(View.java:4106)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.view.View$PerformClick.run(View.java:17052)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.os.Handler.handleCallback(Handler.java:615)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.os.Looper.loop(Looper.java:137)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.app.ActivityThread.main(ActivityThread.java:5059)
04-20 13:46:57.639: E/AndroidRuntime(26747): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 13:46:57.639: E/AndroidRuntime(26747): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 13:46:57.639: E/AndroidRuntime(26747): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-20 13:46:57.639: E/AndroidRuntime(26747): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-20 13:46:57.639: E/AndroidRuntime(26747): at dalvik.system.NativeStart.main(Native Method)
04-20 13:46:57.639: E/AndroidRuntime(26747): Caused by: java.lang.NoSuchMethodException: muscleGroups [class android.view.View]
04-20 13:46:57.639: E/AndroidRuntime(26747): at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-20 13:46:57.639: E/AndroidRuntime(26747): at java.lang.Class.getMethod(Class.java:915)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.view.View$1.onClick(View.java:3580)
04-20 13:46:57.639: E/AndroidRuntime(26747): ... 11 more
04-20 13:46:59.202: I/Process(26747): Sending signal. PID: 26747 SIG: 9
Probably something pretty simple I'm just not aware of yet, but any help would be appreciated!!!!
By looking at your comment I guess that you have not created JAVA files for other Activities.
For e.g. you must create BrowseExercise.java file in the directory where your main Activity lies. And then you need to define this Activity in the AndroidManifest.xml file.
An easy way to do is by just clicking +(or something like that) on left TOP corner , then Android , New Activity . Then you just have to select which type of activity you want to use .
Have you made Java files for all your activity? Like if you are opening other activity it should fulfill following requirements:
It should be mention in manifest file with proper action
There should be one java file corresponding to that activity
Layout file should be defined or you should create layout via code
Intent should be properly declared.
If still problem exists please add more code of your intent calling and java files.
Related
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.
I'm using 2 phones for debuging and 1 phone is showing my activity fine and the other dose the error, how can I repair it + how can I handle it for another users to not have crashes?
the crash:
07-19 19:51:33.614 18679-18679/itay.finci.org.allerwarn E/AndroidRuntime: FATAL EXCEPTION: main
Process: itay.finci.org.allerwarn, PID: 18679
java.lang.RuntimeException: Unable to start activity ComponentInfo{itay.finci.org.allerwarn/itay.finci.org.allerwarn.intro.IntroActivity}: android.view.InflateException: Binary XML file line #38: Error inflating class com.miz.introactivity.NextDoneButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class com.miz.introactivity.NextDoneButton
edit:
thanks for AndroidStudio decompiler I adding the done.xml that dose all the errors.
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="36dp"
android:height="36dp"
android:viewportHeight="48"
android:viewportWidth="48">
<group android:name="done_group">
<path
android:name="done"
android:fillColor="#android:color/white"
android:pathData="M18 32.34L9.66 24l-2.83 2.83L18 38l24-24-2.83-2.83z" />
</group>
</vector>
edit 2:
the layout xml out of the library:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<FrameLayout
android:layout_width="match_parent"
android:paddingLeft="#dimen/title_padding_left"
android:paddingStart="#dimen/title_padding_left"
android:paddingRight="#dimen/title_padding_right"
android:paddingEnd="#dimen/title_padding_right"
android:layout_height="#dimen/navigation_height"
android:layout_gravity="bottom">
<Button
android:id="#+id/skip_button"
android:layout_gravity="center_vertical|left|start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/skip"
android:textColor="#color/skip_text_color"
style="?attr/borderlessButtonStyle"
/>
<LinearLayout
android:id="#+id/progress_layout"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.miz.introactivity.NextDoneButton
android:id="#+id/next_button"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center_vertical|right|end"
android:background="#drawable/next_done_button_selector"
/>
</FrameLayout>
</merge>
its an error in your IntroActivity. InflateException: Binary XML file line #38: Error inflating class com.miz.introactivity.NextDoneButton
More than likely; this could be due to any LARGE images you are using and the device is simply out of memory to display it. Try resizing the image to make the size of it smaller
I'm trying to load a fragment in my Main Activity which will become one of my navigation tabs later. the problem is that the app crashes and says that the fragment cannot be inflated.
I found other similar questions but none of the solutions could help me
Here's my code
MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.projecttabs.MainActivity">
<fragment
android:id="#+id/fragment1"
android:name="com.example.projecttabs.FragmentAbout"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp" />
</LinearLayout>
FrgagmentAbout.java
public class FragmentAbout extends Fragment {
public View OnCreateView(LayoutInflater l, ViewGroup v, Bundle savedInstanceState){
return l.inflate(R.layout.fragment_about,v,false);
}
}
fragment_about.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="182dp"
android:text="ButtonTest" />
</RelativeLayout>
logCat
FATAL EXCEPTION: main
Process: com.example.projecttabs, PID: 1709
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projecttabs/com.example.projecttabs.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1928)
at com.example.projecttabs.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
... 11 more
Caused by: java.lang.IllegalStateException: Fragment com.example.projecttabs.FragmentAbout did not create a view.
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
For what I understand, MainActivity class sets the contentView to main_activity layout, then, in main_activity.xml file, FragmentAbout class is called to replace the fragment by fragment_about layout. am I right?
please, help me!
thanks!
It should be onCreateView() with a lowercase 'o' in FragmentAbout
In the future, try using the java annotation #Override to avoid this stupid bugs
I have the launch activity called MainActivity that will eventually start another called SearchResultsActivity.
They both extend ActionBarActivity, and the second is yet to be written, for now it is just a stub with:
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.search_results);
}
The layout XML is as follows, just a ListView in a white background:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_products"
android:layout_gravity="center_horizontal" />
</LinearLayout>
The app is crashing on setContentView() with the following logcat:
17348-17348/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.SearchResultsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3714)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
at com.example.myapp.SearchResultsActivity.onCreate(SearchResultsActivity.java:42) <-- this is setContentView()
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1630)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3714)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Here's the manifest excerpt:
<activity
android:name="com.example.myapp.MainActivity"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Styled">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myapp.SearchResultsActivity"
android:label="#string/app_name"
android:theme="#style/Theme.Styled"
android:configChanges="orientation|keyboardHidden">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapp.MainActivity" />
</activity>
I thought it could be some issue with the layout, but then I decide to replace R.layout.search_results with R.layout.home, which is the same XML used in MainActivity, therefore should work. But it crashes in the very same line.
Any ideas on what could be causing this?
As others have pointed out, your error is likely in your onCreate code for your second activity. If you read the documentation here you'll see they start their activity off with onCreate and an immediate call back to super.onCreate(...)
Your code should be as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //added this line
setContentView(R.layout.search_results);
}
If you chose to have your IDE automatically generate an Activity stub, it will always place the call to super.onCreate in your activity. It initializes the activity, see this question for more of an explanation.
If this doesn't fix your problem and you have more code in your second activity, it might be useful to post code for the entire activity.
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>