I really don't know whats wrong with this...
The app should only change the text of the text view when clicking a button, in this case the start button.
When I start the app with the virtual machine it crashes.
My code
package com.example.projectiii;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Button btnS = (Button) findViewById(R.id.btStart);
btnS.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
TextView word = (TextView) findViewById(R.id.display);
word.setText("Project");
}
});
}
#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;
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
My xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.projectiii.MainActivity$PlaceholderFragment" >
<EditText
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/display"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/text"
android:layout_below="#+id/text"
android:layout_marginTop="42dp"
android:text="Start" />
<Button
android:id="#+id/btFinish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btStart"
android:layout_alignRight="#+id/text"
android:text="Finish" />
<Chronometer
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/text"
android:layout_marginTop="40dp"
android:text="Chronometer" />
<TextView
android:id="#+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/time"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Text"
android:textSize="30sp" />
My logcat
05-03 15:06:48.720: E/AndroidRuntime(921): FATAL EXCEPTION: main
05-03 15:06:48.720: E/AndroidRuntime(921): Process: com.example.projectiii, PID: 921
05-03 15:06:48.720: E/AndroidRuntime(921): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectiii/com.example.projectiii.MainActivity}: java.lang.NullPointerException
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.os.Looper.loop(Looper.java:136)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 15:06:48.720: E/AndroidRuntime(921): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 15:06:48.720: E/AndroidRuntime(921): at java.lang.reflect.Method.invoke(Method.java:515)
05-03 15:06:48.720: E/AndroidRuntime(921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 15:06:48.720: E/AndroidRuntime(921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 15:06:48.720: E/AndroidRuntime(921): at dalvik.system.NativeStart.main(Native Method)
05-03 15:06:48.720: E/AndroidRuntime(921): Caused by: java.lang.NullPointerException
05-03 15:06:48.720: E/AndroidRuntime(921): at com.example.projectiii.MainActivity.onCreate(MainActivity.java:33)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.Activity.performCreate(Activity.java:5231)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-03 15:06:48.720: E/AndroidRuntime(921): ... 11 more
05-03 15:06:48.760: W/ActivityManager(385): Force finishing activity com.example.projectiii/.MainActivity
Make sure the xml you posted is activity_main, not fragment_main. You don't need a Fragment here, so remove all Fragment-related code.
Bind the view of TextView here:
setContentView(R.layout.activity_main);
TextView word = (TextView) findViewById(R.id.display);
the layout xml you have shown is of the fragment(since it doesn't contain the R.id. container). You are trying to access the button element defined in your Fragment layout in your activity which is giving you he null pointer. Try accessing the button from the fragment class which will resolve your error.
Related
package kdev.circles;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ca = (EditText)findViewById(R.id.ca);
final EditText cp = (EditText)findViewById(R.id.cp);
final EditText sv = (EditText)findViewById(R.id.sv);
final EditText r = (EditText)findViewById(R.id.radius);
final TextView log = (TextView)findViewById(R.id.log);
Button clear = (Button)findViewById(R.id.clear);
Button solve = (Button)findViewById(R.id.Solve);
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ca.setText("");
cp.setText("");
sv.setText("");
r.setText("");
}
});
solve.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if((r.toString()).equals("")) {
log.setText(log.getText() + "\n" + "error, no value entered");
}else{
sv.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText())) * Double.valueOf(String.valueOf(r.getText())) * Double.valueOf(String.valueOf(r.getText())) * 4 / 3 * 3.14159265));
cp.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText()))*3.14159265*2));
ca.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText()))*Double.valueOf(String.valueOf(r.getText()))*3.14159265));
log.setText(log.getText()+"\n"+"ca"+ca.getText()+"\n"+"sv"+sv.getText()+"\n"+"cp"+cp.getText());
}
}
});
}
#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_main, 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);
}
}
xml file:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Solve"
android:id="#+id/Solve"
android:width="100dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/sv" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/radius"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="49dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignStart="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText3"
android:layout_below="#+id/editText2"
android:layout_alignStart="#+id/editText2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="circle's perimeter"
android:id="#+id/cp"
android:layout_above="#+id/editText2"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="circle's area"
android:id="#+id/ca"
android:layout_above="#+id/editText3"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="sphere's volume"
android:id="#+id/sv"
android:layout_alignBottom="#+id/editText3"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="radius"
android:id="#+id/textView3"
android:layout_alignBottom="#+id/radius"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear"
android:id="#+id/clear"
android:layout_alignBottom="#+id/Solve"
android:layout_alignStart="#+id/editText3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/log"
android:layout_below="#+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
Everything is working fine till i try opening it, i couldn't find the error, because my android virtual device wasn't connected to android studio properly. I am not sure what i should do. I did have another problem before but i realised that it was due to me using the findviewbyid function before the whole layout was loaded. I have solved that problem and tried a few different techniques to solve this problem but nothing worked.
When i try and run the app it just crashes, i am not sure why?
Any help will be usefull, thank you.
this is the logcat:
12-10 15:21:18.030 1907-1907/kdev.circles W/art﹕ Verification of java.lang.CharSequence android.support.v7.widget.ResourcesWrapper.getQuantityText(int, int) took 123.881364ms
12-10 15:21:18.520 1907-1907/kdev.circles D/AndroidRuntime﹕ Shutting down VM
12-10 15:21:18.520 1907-1907/kdev.circles E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: kdev.circles, PID: 1907
java.lang.RuntimeException: Unable to start activity ComponentInfo{kdev.circles/kdev.circles.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at kdev.circles.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-10 15:21:27.230 1907-1907/kdev.circles I/Process﹕ Sending signal. PID: 1907 SIG: 9
12-10 15:21:31.360 2153-2153/kdev.circles D/AndroidRuntime﹕ Shutting down VM
12-10 15:21:31.360 2153-2153/kdev.circles E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: kdev.circles, PID: 2153
java.lang.RuntimeException: Unable to start activity ComponentInfo{kdev.circles/kdev.circles.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at kdev.circles.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Check your view types in Java code and their matching id's in xml. They have to be of same type.
You have EditText in Java, but matching view id in xml is TextView
For instance:
final EditText ca = (EditText)findViewById(R.id.ca);
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="circle's area"
android:id="#+id/ca"
android:layout_above="#+id/editText3"
android:layout_alignParentStart="true" />
My app get stops while including particular java class in manifest.xml and the logcat shows error in fragment.If i remove those classes from manifest.xml the app is running successfully.On the other side remaining java classes working properly and there is no error in the fragment.I don't know why the app gets stopped for the particular java files
This is the error shown by logcat while including that particular java class
10-05 10:46:31.617 12820-12820/com.sentientit.theiWedplanner E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sentientit.theiWedplanner/com.sentientit.theiWedplanner.Frontpage}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
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:350)
at android.app.Activity.setContentView(Activity.java:1930)
at com.sentientit.theiWedplanner.Frontpage.onCreate(Frontpage.java:41)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.sentientit.theiWedplanner.Fragadmob: make sure class name exists, is public, and has an empty constructor that is public
at android.app.Fragment.instantiate(Fragment.java:592)
at android.app.Fragment.instantiate(Fragment.java:560)
at android.app.Activity.onCreateView(Activity.java:4864)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
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:350)
at android.app.Activity.setContentView(Activity.java:1930)
at com.sentientit.theiWedplanner.Frontpage.onCreate(Frontpage.java:41)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sentientit.theiWedplanner.Fragadmob" on path: /system/framework/com.google.android.maps.jar:/data/app/com.sentientit.theiWedplanner-1.apk
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Fragment.instantiate(Fragment.java:582)
at android.app.Fragment.instantiate(Fragment.java:560)
at android.app.Activity.onCreateView(Activity.java:4864)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
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:350)
at android.app.Activity.setContentView(Activity.java:1930)
at com.sentientit.theiWedplanner.Frontpage.onCreate(Frontpage.java:41)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
10-05 10:46:40.273 12820-12824/com.sentientit.theiWedplanner D/dalvikvm﹕ GC_CONCURRENT freed 413K, 28% free 6025K/8348K, paused 4ms+2ms, total 36ms
front.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<fragment
android:id="#+id/adview153613"
android:name="com.sentientit.theiWedplanner.Fragadmob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#layout/fragadmob" />
<RelativeLayout
android:id="#+id/fron"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="50px"
android:background="#003399"
android:gravity="center"
android:text="iWedPlanner"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<ImageView
android:id="#+id/cl"
android:layout_width="match_parent"
android:layout_height="600dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:src="#drawable/ilogo" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="23dp"
android:gravity="center"
android:text="Welcome to iWedPlanner"
android:textColor="#FF9933"
android:textSize="20sp"
android:textStyle="italic"
android:typeface="serif" />
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cl"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="Your Guide to fun and flawless wedding planning"
android:textColor="#FFFF0000"
android:textStyle="italic"
android:typeface="serif" />
<ImageButton
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cl"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="#FFFFFF"
android:src="#drawable/ib" />
</RelativeLayout>
</LinearLayout>
fragadmob.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adview153613"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/aagr_background2" >
<ImageView
android:id="#+id/image123"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:src="#drawable/adview1536132" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="XXXXXXXXXXXXXXXXXXXXXXXXXXX"
/>
<ImageView
android:id="#+id/addviewimage1"
android:layout_width="31dp"
android:layout_height="31dp"
android:layout_alignParentRight="true"
android:layout_marginRight="23dp"
android:src="#drawable/close" />
</RelativeLayout>
frontpage.java
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import com.bugsense.trace.BugSenseHandler;
public class Frontpage extends FragmentActivity {
/** Called when the activity is first created. */
SharedPreferences sharedPreferences;
#Override
public void onStart() {
super.onStart();
// The rest of your onStart() code.
// // EasyTracker.getInstance(this).activityStart(this); // Add this method.
}
#Override
public void onStop() {
super.onStop();
// The rest of your onStop() code.
// EasyTracker.getInstance(this).activityStop(this); // Add this method.
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); BugSenseHandler.initAndStartSession(this, "68640bea");
//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.front);
RelativeLayout clic =(RelativeLayout)findViewById(R.id.fron);
clic.setOnClickListener(new OnClickListener() {
//#Override
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent=new Intent(Frontpage.this,welcomeuser1.class);
//Intent intent=new Intent(Frontpage.this,ZXingJarDemoActivity.class);
startActivity(intent);
finish();
}
});
}
}
fragadmob.java
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class Fragadmob extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragadmob, container, false);
//Admob
RelativeLayout gonad=(RelativeLayout)rootView.findViewById(R.id.adview153613);
ImageView addviewimage=(ImageView)rootView.findViewById(R.id.image123);
ImageView inap=(ImageView)rootView.findViewById(R.id.addviewimage1);
// AdView adView = new AdView(getActivity());
AdView adView = (AdView)rootView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
if(addviewimage.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
addviewimage.getLayoutParams().height = 30;
// code to do for Portrait Mode
}
return rootView;
}
}
You never need to add fragments to your manifest. your logcat showing ClassNotFoundException: Didn't find class "com.sentientit.theiWedplanner.Fragadmob" but Fragadmob extends Fragment so u trying to adding fragment in manifast so make sure once and enjoy coding.
I am trying to add the abmob ads to an activity in my android app but the the fragment will not load. When the application load it crash and say's Unable to start activity ComponentInfo{catchmedia.jamaica.dictionary/catchmedia.jamaica.dictionary.HomeActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment. what is that i have done wrong?
stacktrace
01-06 22:28:02.335 1356-1356/catchmedia.jamaica.dictionary E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: catchmedia.jamaica.dictionary, PID: 1356
java.lang.RuntimeException: Unable to start activity ComponentInfo{catchmedia.jamaica.dictionary/catchmedia.jamaica.dictionary.HomeActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #15: 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:1929)
at catchmedia.jamaica.dictionary.HomeActivity.onCreate(HomeActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030004
at android.content.res.Resources.getValue(Resources.java:1123)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)
at android.content.res.Resources.getLayout(Resources.java:939)
at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
at catchmedia.jamaica.dictionary.HomeActivity$AdFragment.onCreateView(HomeActivity.java:156)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
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:1929)
at catchmedia.jamaica.dictionary.HomeActivity.onCreate(HomeActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
01-06 22:28:04.183 1356-1356/catchmedia.jamaica.dictionary I/Process﹕ Sending signal. PID: 1356 SIG: 9
AdFragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--ads:adUnitId sets the ad unit ID, which is defined in values/strings.xml -->
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id" />
</RelativeLayout>
Xml Code
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<fragment
android:id="#+id/adFragment"
android:name="catchmedia.jamaica.dictionary.HomeActivity$AdFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
Class
package catchmedia.jamaica.dictionary;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import java.util.List;
import catchmedia.jamaica.dictionary.adapter.TabsPagerAdapter;
import database.DatabaseHandler;
import database.DatabaseInfo;
import database.Word;
public class HomeActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter tabsPagerAdapter;
private ActionBar actionBar;
DatabaseHandler db = new DatabaseHandler(this);
private String[] tabsTitles = {"Lesson", "Word"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initializeDatabase();
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
tabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabsPagerAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabsTitles) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
public void initializeDatabase() {
List<Word> words = db.getAllWords();
if (words.size() == 0) {
DatabaseInfo info = new DatabaseInfo(db);
info.insertWords();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return super.onCreateOptionsMenu(menu);
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
default:
return super.onOptionsItemSelected(item);
}
}
public static class AdFragment extends Fragment {
private AdView mAdView;
public AdFragment() {
}
#Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mAdView.loadAd(adRequest);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ad, container, false);
}
/** Called when leaving the activity */
#Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
/** Called when returning to the activity */
#Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
/** Called before the activity is destroyed */
#Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
}
}
In your HomeActivity class, you've imported android.support.v4.app.Fragment, android.support.v4.app.FragmentActivity, etc. There is a conflict here.
In your XML, you're using <fragment>. You need to either change your imports to android.app.Fragment, or change XML to:
<android.support.v4.app.fragment
android:id="#+id/adFragment"
android:name="catchmedia.jamaica.dictionary.HomeActivity$AdFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
If you're using fragments, and want to target devices before API 11, you should be using use android.support.v4.app.Fragment. However, if you're only targeting devices running API 11 or above, you can use android.app.Fragment.
Otherwise, there is a conflict between you using Fragment and Fragment from the Android support library.
You should change fragment to android.support.v4.Fragment in your xml file as your AdFragment extends from android.support.v4.Fragment.
This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
When I try to open this page it crashes. I have tried to make this button count various ways and can't get it to count! I have tried all kinds of tutorials by thenewboston and other people but it doesn't work for me.
Medical.java
package com.fullcyclestudios.rust;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
public class Medical extends ActionBarActivity {
Button add, sub;
int counter;
TextView textView;
TextView quantityDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medical);
add=(Button) findViewById(R.id.addButtonMedical);
sub=(Button) findViewById(R.id.minusButtonMedical);
quantityDisplay = (TextView) findViewById(R.id.textViewQuantityMedical);
counter=1;
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
counter++;
quantityDisplay.setText("it"+counter);
}
});
sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
counter--;
quantityDisplay.setText("it"+counter);
}
});
Intent intent = getIntent();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.medical, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_medical,
container, false);
return rootView;
}
}
/** Bandage button */
public String bandageMaterials(View view)
{
String materials = "Bandage\r\n\r\n2 Cloth";
textView = (TextView) findViewById(R.id.textViewMatMedical);
textView.setText(materials);
return materials;
}
/** Small Medkit button */
public String smallMedkitMaterials(View view)
{
String materials = "Small Medkit\r\n\r\n2 Cloth\r\n2 Blood";
textView = (TextView) findViewById(R.id.textViewMatMedical);
textView.setText(materials);
return materials;
}
/** Large Medkit button */
public String largeMedkitMaterials(View view)
{
String materials = "Large Medkit\r\n\r\n3 Cloth\r\n3 Blood";
textView = (TextView) findViewById(R.id.textViewMatMedical);
textView.setText(materials);
return materials;
}
}
fragment_medical.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.fullcyclestudios.rust.Medical$PlaceholderFragment" >
<ScrollView
android:id="#+id/scrollViewMedical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/bandageButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="35dp"
android:text="#string/bandage"
android:onClick="bandageMaterials" />
<Button
android:id="#+id/smallMedkitButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="35dp"
android:text="#string/smallMedkit"
android:onClick="smallMedkitMaterials" />
<Button
android:id="#+id/largeMedkitButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="35dp"
android:text="#string/largeMedkit"
android:onClick="largeMedkitMaterials" />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/textViewMatMedical"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:layout_marginTop="140dp"
android:background="#color/gray"
android:text="#string/blank" />
<TextView
android:id="#+id/textViewQuantityMedical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/minusButtonMedical"
android:layout_alignRight="#+id/minusButtonMedical"
android:layout_below="#+id/addButtonMedical"
android:gravity="center_horizontal"
android:text="#string/one"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="25sp" />
<Button
android:id="#+id/minusButtonMedical"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="80dp"
android:layout_alignLeft="#+id/addButtonMedical"
android:layout_alignRight="#+id/addButtonMedical"
android:text="#string/minus"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="#+id/addButtonMedical"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignLeft="#+id/textViewMatMedical"
android:layout_alignRight="#+id/textViewMatMedical"
android:text="#string/add"
android:textSize="30sp" />
</RelativeLayout>
ERRORS
06-08 16:10:38.890: W/dalvikvm(855): threadid=1: thread exiting with uncaught exception (group=0xb2a40ba8)
06-08 16:10:38.990: E/AndroidRuntime(855): FATAL EXCEPTION: main
06-08 16:10:38.990: E/AndroidRuntime(855): Process: com.fullcyclestudios.rust, PID: 855
06-08 16:10:38.990: E/AndroidRuntime(855): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fullcyclestudios.rust/com.fullcyclestudios.rust.Medical}: java.lang.NullPointerException
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.os.Handler.dispatchMessage(Handler.java:102)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.os.Looper.loop(Looper.java:136)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-08 16:10:38.990: E/AndroidRuntime(855): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 16:10:38.990: E/AndroidRuntime(855): at java.lang.reflect.Method.invoke(Method.java:515)
06-08 16:10:38.990: E/AndroidRuntime(855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-08 16:10:38.990: E/AndroidRuntime(855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-08 16:10:38.990: E/AndroidRuntime(855): at dalvik.system.NativeStart.main(Native Method)
06-08 16:10:38.990: E/AndroidRuntime(855): Caused by: java.lang.NullPointerException
06-08 16:10:38.990: E/AndroidRuntime(855): at com.fullcyclestudios.rust.Medical.onCreate(Medical.java:33)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.Activity.performCreate(Activity.java:5231)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-08 16:10:38.990: E/AndroidRuntime(855): ... 11 more
06-08 16:10:43.330: I/Process(855): Sending signal. PID: 855 SIG: 9
Your issue is here:
setContentView(R.layout.activity_medical);
add=(Button) findViewById(R.id.addButtonMedical);
sub=(Button) findViewById(R.id.minusButtonMedical);
but from what you post: addButtonMedical and minusButtonMedical are inside fragment_medical and not activity_medical xml layout!
when try to click on New Activity Button in my MainActivity , my app crash and closed . whit this error on screen " unfortunately[ App Name ] has stopped "
I google this problem and i can't find any Definitive solution.
here is logCat Log , First Java Source ( MainActivity.java ) and seccond java source ( Seccond.java ) .
Logcat Log :
05-03 00:05:17.750: E/AndroidRuntime(1091): FATAL EXCEPTION: main
05-03 00:05:17.750: E/AndroidRuntime(1091): Process: com.example.helloworld, PID: 1091
05-03 00:05:17.750: E/AndroidRuntime(1091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.Seccond}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment{b2d2faf8 #0 id=0x7f05003c}
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.os.Looper.loop(Looper.java:136)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 00:05:17.750: E/AndroidRuntime(1091): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 00:05:17.750: E/AndroidRuntime(1091): at java.lang.reflect.Method.invoke(Method.java:515)
05-03 00:05:17.750: E/AndroidRuntime(1091): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 00:05:17.750: E/AndroidRuntime(1091): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 00:05:17.750: E/AndroidRuntime(1091): at dalvik.system.NativeStart.main(Native Method)
05-03 00:05:17.750: E/AndroidRuntime(1091): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment{b2d2faf8 #0 id=0x7f05003c}
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.Activity.performStart(Activity.java:5241)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
MainActivity.java
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
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.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Start
Button btn_img = (Button) findViewById(R.id.btn_image);
Button btn = (Button) findViewById(R.id.btn_Click);
final TextView txt = (TextView) findViewById(R.id.txt_view);
Button btn_avtivity = ( Button) findViewById(R.id.btn_activity);
// For button click
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
txt.setText(" Amin Bassam ");
}
});
// Image show
btn_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
ImageView img=(ImageView)findViewById(R.id.img_view);
img.setImageResource(R.drawable.amin);
}
});
btn_avtivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(),Seccond.class);
startActivity(intent);
}
});
// Open Activities
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new Fragment()).commit();
}
}
#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;
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
}
Seccond.java
package com.example.helloworld;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class Seccond extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seccond);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.seccond, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_seccond,
container, false);
return rootView;
}
}
}
Activity_main.Xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.helloworld.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/txt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btn_Click"
android:layout_alignParentTop="true"
android:layout_marginTop="98dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btn_Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="66dp"
android:text="Click"
tools:ignore="HardcodedText" />
"res/layout/activity_main.xml"
<Button
android:id="#+id/btn_image"
style="#style/AppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_Click"
android:layout_alignBottom="#+id/btn_Click"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/btn_Click"
android:text="Image"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/img_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btn_image"
android:layout_centerVertical="true"
android:scaleType="center"
android:src="#drawable/abc_ab_solid_light_holo"
tools:ignore="ContentDescription" />
<Button
android:id="#+id/btn_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btn_image"
android:text="New Activities"
tools:ignore="HardcodedText" />
</RelativeLayout>
Seccond_activity.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="#A8B007"
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.helloworld.Seccond$PlaceholderFragment" >
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.helloworld.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.helloworld.Seccond"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment
The error is saying it cannot find the view with an id of container for the fragment placeholder to go into.
setContentView(R.layout.activity_seccond);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
This part of the code is the main problem. In the add() for the getSupportFragmentManager, it is saying to create the fragment and put it in a view with the id container. But the layout specified in setContentView does not have a view with an id of container. So there needs to be a layout file with a View (like a frame) with an id of container, so that when the fragment layout loads, it can go into that view.
Try adding a file to layouts called activity_frame_seccond.xml with:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Which is a frame with the id 'container'.
Change this line in Seccond.java:
setContentView(R.layout.activity_seccond);
for
setContentView(R.layout.activity_frame_seccond);
and see if that works.
With this code you are using the new file activity_frame_seccond.xml as the layout for the activity, and the first file activity_seccond.xml as the fragment that will put in the frame element (because it has the id container).
If this doesn't work let me know and I will have another look.
Check this line in your Fragment class :
View rootView = inflater.inflate(R.layout.activity_seccond,
container, false);
i think the Returned View is Null
Remove this line from your manifest
<activity
android:name="com.example.helloworld.Second"
android:label="#string/app_name" >
>
</activity>
and try
try in this way
#Override
public void onClick(View arg0) {
Intent intent = new Intent(this,Seccond.class);
startActivity(intent);
}