I have been trying to use the addTextChangedListener for three days, The log says that it is caused by java.lang.NullPointerException. When I comment the addTextChangedListener block the program runs fine.
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class calc extends ActionBarActivity {
EditText text;
private TextView textout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calc);
text = (EditText) findViewById(R.id.fs);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
text.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calc, 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_calc, container, false);
return rootView;
}
}
}
And here we have the log.
01-09 16:03:12.280 1088-1088/com.fuzato.buckconverter E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fuzato.buckconverter, PID: 1088
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fuzato.buckconverter/com.fuzato.buckconverter.calc}: java.lang.NullPointerException
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: java.lang.NullPointerException
at com.fuzato.buckconverter.calc.onCreate(calc.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)
The log says that the problem is found in onCreate and it is caused by a java.lang.NullPointerException.
Any thoughts?
Thanks for everything!
Layout:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:clickable="false">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="false"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="61dp"
android:layout_marginTop="43dp"
android:id="#+id/gridLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switching Freq. (Fs)"
android:id="#+id/fs"
android:layout_row="0"
android:layout_column="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Output Voltage (V2)"
android:id="#+id/V2"
android:layout_row="1"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/V2"
android:layout_row="1"
android:layout_column="1"
android:text="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Volts"
android:id="#+id/textView12"
android:layout_row="1"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input Voltage (V1)"
android:id="#+id/textView3"
android:layout_row="2"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/V1"
android:layout_row="2"
android:layout_column="1"
android:text="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Volts"
android:id="#+id/textView13"
android:layout_row="2"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Duty Cycle (k)"
android:id="#+id/k"
android:layout_row="3"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/k"
android:layout_row="3"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Ripple (ΔI)"
android:id="#+id/textView5"
android:layout_row="4"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/deltaI"
android:layout_row="4"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Amps"
android:id="#+id/textView15"
android:layout_row="4"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Voltage Ripple (ΔV)"
android:id="#+id/textView6"
android:layout_row="5"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/deltaV"
android:layout_row="5"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Volts"
android:id="#+id/textView16"
android:layout_row="5"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max current (Imax)"
android:id="#+id/Imax"
android:layout_row="6"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/Imax"
android:layout_row="6"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Amps"
android:id="#+id/textView17"
android:layout_row="6"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Min Current (Imin)"
android:id="#+id/Imin"
android:layout_row="7"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/Imin"
android:layout_row="7"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Amps"
android:id="#+id/textView18"
android:layout_row="7"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inductance (L)"
android:id="#+id/Ind"
android:layout_row="8"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/L"
android:layout_row="8"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mH"
android:id="#+id/textView19"
android:layout_row="8"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capacitance (C)"
android:id="#+id/cap"
android:layout_row="9"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/fs"
android:layout_row="0"
android:layout_column="1"
android:text="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hz"
android:id="#+id/textView11"
android:layout_row="0"
android:layout_column="2" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/C"
android:layout_row="9"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="uF"
android:id="#+id/textView20"
android:layout_row="9"
android:layout_column="2" />
</GridLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/gridLayout"
android:id="#+id/relativeLayout">
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="apply"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/enter"
android:layout_below="#+id/relativeLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="94dp"
android:text="3" />
</RelativeLayout>
I have also tried to debbug it in my phone, I get the same problem.
I'm not entirely sure why you app compiles in the first place, but the problem initially was that your findViewById() method was returning null because it couldn't find a view matching your ID.
Your current issues include:
- Your text object is declared as TextView while it should be an
EditText
- In your layout, your view with id R.id.fs is a TextView when it should be an EditText.
try change TextView text; to EditText text;
I found the problem.
My layout was on fragment_main, sorry!
Thanks for the help!!
Related
I am relatively new to coding but have some experience in Java and Android. I have a really basic problem that I have done over and over again but for some reason, this occurrence doesn't work!
I have an edittext field which is populated by the user. When an enter button is pressed the app then takes this and saves it as a String. The String is then used to populate the TextView with the user input.
I have created the findviewbyids, I have set the String to equal the edittext input and then set the text in the text view to be the String as per the code below. I have checked previous apps I have made and this has always worked before...
package com.example.android.golfhandicap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText playerName;
TextView playerOneName;
String name;
int handicap;
EditText playerHandicap;
TextView playerOneHcp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
}
//set view by IDs
public void views() {
playerName = findViewById(R.id.inputPlayerName);
playerOneName = findViewById(R.id.playerOneName);
playerHandicap = findViewById(R.id.inputPlayerHcp);
playerOneHcp = findViewById(R.id.playerOneHcp);
}
/**
* Add the codes for the button to navigate around the app
*/
public void enterScore1(View view) {
setContentView(R.layout.front_page);
}
public void addPlayer(View view) {
setContentView(R.layout.new_player);
}
public void addScore(View view) {
setContentView(R.layout.input_page);
}
public void enterPlayer(View view) {
views();
name = playerName.getText().toString();
//handicap = Integer.parseInt(playerHandicap.getText().toString());
playerOneName.setText(name);
//playerOneHcp.setText(handicap);
Toast.makeText(this, "player name is " + name + " and handicap is
" + handicap , Toast.LENGTH_SHORT).show();
setContentView(R.layout.front_page);
}
}
The XML for the page with the text view I want to display the String in:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:visibility="visible"
tools:context=".MainActivity">
<TextView
android:id="#+id/appName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="Golf handicap"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible"
android:editable="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/playerOneName"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:editable="false"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appName2" />
<TextView
android:id="#+id/playerOneName2"
android:editable="false"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/playerOneName3" />
<TextView
android:id="#+id/playerOneName3"
android:editable="false"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/playerOneName" />
<Button
android:id="#+id/addScore"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:background="#android:color/holo_green_light"
android:onClick="addScore"
android:text="Add new score"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/addPlayer" />
<Button
android:id="#+id/addPlayer"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:background="#android:color/holo_green_light"
android:onClick="addPlayer"
android:text="Add new player"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/addScore"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/playerOneHcp"
android:layout_width="43dp"
android:layout_height="30dp"
android:layout_marginStart="31dp"
android:layout_marginTop="32dp"
android:editable="false"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:visibility="visible"
app:layout_constraintStart_toEndOf="#+id/playerOneName"
app:layout_constraintTop_toBottomOf="#+id/appName2" />
<TextView
android:id="#+id/editText2"
android:editable="false"
android:layout_width="43dp"
android:layout_height="30dp"
android:layout_marginStart="31dp"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="#+id/playerOneName2"
app:layout_constraintTop_toBottomOf="#+id/editText3" />
<TextView
android:id="#+id/editText3"
android:editable="false"
android:layout_width="43dp"
android:layout_height="30dp"
android:layout_marginStart="31dp"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="#+id/playerOneName3"
app:layout_constraintTop_toBottomOf="#+id/playerOneHcp" />
</android.support.constraint.ConstraintLayout>
The XML for the layout with the edittext which I get the user input from:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<TextView
android:id="#+id/enterHcpText"
android:layout_width="136dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:editable="false"
android:text="Current handicap :"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enterAgeText" />
<TextView
android:id="#+id/enterAgeText"
android:layout_width="136dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:editable="false"
android:text="Player age :"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enterNameText" />
<TextView
android:id="#+id/enterNameText"
android:layout_width="136dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:editable="false"
android:text="Player name :"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appName3" />
<TextView
android:id="#+id/appName3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:editable="false"
android:text="Golf handicap"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/inputPlayerName"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="33dp"
android:maxLength="25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appName3" />
<EditText
android:id="#+id/inputPlayerAge"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="33dp"
android:inputType="number"
android:maxLength="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputPlayerName" />
<EditText
android:id="#+id/inputPlayerHcp"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="33dp"
android:inputType="number"
android:maxLength="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputPlayerAge" />
<Button
android:id="#+id/enterPlayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="96dp"
android:layout_marginEnd="148dp"
android:onClick="enterPlayer"
android:text="Enter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputPlayerHcp" />
</android.support.constraint.ConstraintLayout>
If I comment out the line p1Name.setText(name); then it works fine, I have a toast to check the name is taken and name is set as the input. So there seems to be something wrong with the p1Name. I have checked and triple checked all the IDs are correct.
I have spent over an hour now trying to figure it out and can't see what I am doing wrong!
This is the lines I get on logcat (These are all the lines shown in red)
2019-01-24 23:29:39.376 9545-9545/com.example.android.golfhandicap E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.golfhandicap, PID: 9545
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.android.golfhandicap.MainActivity.enterPlayer(MainActivity.java:65)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Any help would be gratefully received.
The problem is simply because of the following:
you haven't bind the view with the findViewById. This is because of the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
// Here you haven't bind the view.
}
you should directly bind the view with your views() method. So, change the code to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
// bind the view.
views();
}
You're changing the layout view each time a button is clicked with this method:
public void addScore(View view) {
setContentView(R.layout.input_page);
}
which is become one of the source for your problem. First, it's not recommended because you're redrawing entire layout when calling setContentView. Second, you need to make sure all the layout onClick is implemented in your code.
Last but not the least, you better using setOnClickListener in code instead onClick attribute in layout. It is because you should separate between logic and UI.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
There is the eror message on the monitor:
07-20 17:00:05.234 17194-17194/com.example.champion.courtcounter E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.champion.courtcounter/com.example.champion.courtcounter.MainActivity}: android.view.InflateException: Binary XML file line #69: Error inflating class null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2129)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
at android.app.ActivityThread.access$700(ActivityThread.java:146)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4949)
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:1043)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #69: Error inflating class null
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.champion.courtcounter.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
at android.app.ActivityThread.access$700(ActivityThread.java:146)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4949)
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:1043)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:101)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:681)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.champion.courtcounter.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
at android.app.ActivityThread.access$700(ActivityThread.java:146)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4949)
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:1043)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
at dalvik.system.NativeStart.main(Native Method)
There is my xml code:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Team A"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:paddingBottom="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="points3"
android:text="+3 points" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:onClick="points2"
android:text="+2 points" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="point"
android:text="free throw" />
</LinearLayout>
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="#android:color/darker_gray" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Team B"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:paddingBottom="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="points3B"
android:text="+3 points" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:onClick="points2B"
android:text="+2 points" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="pointB"
android:text="free throw" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="reset"
android:text="reset" />
</RelativeLayout>
There is my java code:
package com.example.champion.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.example.champion.courtcounter.R;
public class MainActivity extends AppCompatActivity {
int numA=0;
int numB=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
public void points3(View view) {
numA = numA + 3;
displayForTeamA(numA);
}
public void points2(View view) {
numA = numA + 2;
displayForTeamA(numA);
}
public void point(View view) {
numA = numA + 1;
displayForTeamA(numA);
}
public void displayForTeamB(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_b_score);
scoreView.setText(String.valueOf(score));
}
public void points3B(View view) {
numB = numB + 3;
displayForTeamB(numB);
}
public void points2B(View view) {
numB = numB + 2;
displayForTeamB(numB);
}
public void pointB(View view) {
numB = numB + 1;
displayForTeamB(numB);
}
public void reset(View view) {
numB = 0;
displayForTeamB(numB);
numA = 0;
displayForTeamA(numA);
}
}
I can't even open it. When I try to open it's says:"Unfortunately, Court Counter(the name of my app) has stopped.
Thank for help.
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="#android:color/darker_gray" />
Change view with View
so, I'm getting an error after trying to load a tab in a Master/Flow layout. The tab that makes the app crash is a calculator with two images and a background image.
Here's the error:
04-23 09:21:40.609 1157-1157/endangeredspecies.androidbootcamp.net.finalproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: endangeredspecies.androidbootcamp.net.finalproject, PID: 1157
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at endangeredspecies.androidbootcamp.net.finalproject.ItemDetailFragment.onCreateView(ItemDetailFragment.java:66)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
And this is where it is telling me the error is (ItemDetailFragment.java:66):
it is "rootView = inflater.inflate(R.layout.price_calculator, container, false);"
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
if (mItem !=null) {
if (mItem.id.equals("1")) {
rootView = inflater.inflate(R.layout.welcome_page, container, false);
} else if (mItem.id.equals("2")) {
rootView = inflater.inflate(R.layout.price_calculator, container, false);
} else if (mItem.id.equals("3")) {
((WebView) rootView.findViewById(R.id.item_Detail)).loadUrl(mItem.item_url);
}
}
return rootView;
}
Here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#mipmap/calc_background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txtCalc"
android:id="#+id/textView2"
android:layout_marginTop="47dp"
android:textSize="50dp"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/textView3"
android:textColor="#ff000000" />
<ImageView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#mipmap/spiderman_1"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="-10dp" />
<ImageView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#mipmap/hulk"
android:layout_marginTop="279dp"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true"
android:layout_marginBottom="0dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txtAdults"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/imageView4"
android:layout_marginTop="111dp"
android:textSize="30dp"
android:textColor="#ff000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txtChildern"
android:id="#+id/textView4"
android:textSize="30dp"
android:layout_below="#+id/textView3"
android:layout_alignStart="#+id/textView3"
android:layout_marginTop="20dp"
android:textColor="#ff000000" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/txtAdults"
android:layout_above="#+id/textView4"
android:layout_toEndOf="#+id/textView4"
android:layout_marginStart="45dp"
android:textColor="#ff000000"
android:hint="#string/txtHint1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/txtChildren"
android:layout_alignBottom="#+id/textView4"
android:layout_alignStart="#+id/txtAdults"
android:textColor="#ff000000"
android:hint="#string/txtHint2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txtDays"
android:id="#+id/textView5"
android:textSize="30dp"
android:layout_below="#+id/textView4"
android:layout_alignStart="#+id/textView4"
android:layout_marginTop="20dp"
android:textColor="#ff000000" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/txtDays"
android:layout_alignTop="#+id/textView5"
android:layout_alignStart="#+id/txtChildren"
android:textColor="#ff000000"
android:hint="#string/txtHint3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txtCalcLbl"
android:id="#+id/btnTotal"
android:layout_marginTop="57dp"
android:hint="#string/txtClickCalc"
android:textSize="20dp"
android:layout_alignTop="#+id/imageView4"
android:layout_toEndOf="#+id/textView4"
android:textColor="#ff000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txtTotal"
android:id="#+id/txtResult"
android:layout_below="#+id/imageView3"
android:layout_alignStart="#+id/btnTotal"
android:textSize="30dp"
android:textColor="#ff000000" />
</RelativeLayout>
Here is the Java Code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_create_report, container, false);
activity = getActivity();
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinnerComplaintType);
ArrayAdapter<String> adapterComplaint = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, R.array.complaintType);
adapterComplaint.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapterComplaint);
spinnerComplaintType.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
complaintType = parent.getItemAtPosition(position).toString();
// Toast.makeText(getApplicationContext(), complaintType, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return rootView;
}
Here is the XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="833dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="24dp"
android:layout_marginTop="54dp"
android:text="Complaint Type: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/spinnerComplaintType"
android:layout_marginTop="15dp"
android:text="Vehicle Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinnerVehicleType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/textView2"
android:textColor="#000000" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/spinnerVehicleType"
android:layout_marginTop="21dp"
android:text="Body No:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editTextBodyNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView4"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextPlate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextBodyNo"
android:layout_alignTop="#+id/textView5"
android:ems="10" />
<EditText
android:id="#+id/editTextDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextLocation"
android:layout_alignTop="#+id/textView7"
android:ems="10" />
<EditText
android:id="#+id/editTextComplaintDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextDate"
android:layout_alignTop="#+id/textView8"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView5"
android:layout_below="#+id/textView5"
android:layout_marginTop="58dp"
android:text="Location"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4"
android:layout_marginTop="55dp"
android:text="Plate No:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/editTextLocation"
android:layout_marginTop="30dp"
android:text="Date of Incident"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/editTextDate"
android:layout_marginTop="28dp"
android:text="Complaint Details"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editTextLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextPlate"
android:layout_alignTop="#+id/textView6"
android:ems="10" />
<ImageView
android:id="#+id/imageViewPhotoReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonCamera"
android:layout_below="#+id/buttonCamera"
android:src="#drawable/about"
android:visibility="invisible"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:text="File Complaint"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/buttonCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonSend"
android:layout_toLeftOf="#+id/spinnerVehicleType"
android:text="Take Photo" />
<Button
android:id="#+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editTextComplaintDetails"
android:layout_marginTop="48dp"
android:layout_toRightOf="#+id/textView1"
android:text="SEND" />
<Button
android:id="#+id/buttonDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextComplaintDetails"
android:layout_below="#+id/editTextComplaintDetails"
android:layout_marginTop="20dp"
android:text="pick date" />
<Spinner
android:id="#+id/spinnerComplaintType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_toRightOf="#+id/textView3"
android:textColor="#000000" />
</RelativeLayout>
</ScrollView>
Here is the Error
Log Trace
01-04 21:18:36.513: E/AndroidRuntime(3772): FATAL EXCEPTION: main
01-04 21:18:36.513: E/AndroidRuntime(3772): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Spinner
01-04 21:18:36.513: E/AndroidRuntime(3772): at info.androidhive.slidingmenu.CreateReportFragment.onCreateView(CreateReportFragment.java:31)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:830)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1034)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.BackStackRecord.run(BackStackRecord.java:622)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1384)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:428)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.os.Handler.handleCallback(Handler.java:605)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.os.Handler.dispatchMessage(Handler.java:92)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.os.Looper.loop(Looper.java:154)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.ActivityThread.main(ActivityThread.java:4945)
01-04 21:18:36.513: E/AndroidRuntime(3772): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 21:18:36.513: E/AndroidRuntime(3772): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 21:18:36.513: E/AndroidRuntime(3772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-04 21:18:36.513: E/AndroidRuntime(3772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-04 21:18:36.513: E/AndroidRuntime(3772): at dalvik.system.NativeStart.main(Native Method)
So I was able to make a Spinner work in a single Activity, but when I transferred the code to a Fragment it obviously didn't work.
I think the Error was from the Instatiation of Spinner.
Use this one for your TextView and Spinner.
<Spinner
android:id="#+id/spinnerComplaintType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="54dp"
android:layout_toRightOf="#+id/buttonSend"
android:textColor="#000000" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignTop="#+id/spinnerComplaintType"
android:layout_marginTop="17dp"
android:text="File Complaint"
android:textAppearance="?android:attr/textAppearanceLarge" />
Also Change
android.R.layout.simple_spinner_item
instead of
android.R.layout.simple_list_item_1
in your Array Adapter.
UPDATE:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.complaintType, android.R.layout.simple_spinner_item);
This is the error i get. You can see all the codes that are affected by this session below. Where i put the intent, where i get it, the classes and paremeters involved.
11-30 20:37:32.870: E/AndroidRuntime(809): FATAL EXCEPTION: main
11-30 20:37:32.870: E/AndroidRuntime(809): java.lang.RuntimeException: Unable to start activity ComponentInfo{kfc.project/kfc.project.productdetail}: java.lang.NullPointerException
11-30 20:37:32.870: E/AndroidRuntime(809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-30 20:37:32.870: E/AndroidRuntime(809): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-30 20:37:32.870: E/AndroidRuntime(809): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-30 20:37:32.870: E/AndroidRuntime(809): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-30 20:37:32.870: E/AndroidRuntime(809): at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 20:37:32.870: E/AndroidRuntime(809): at android.os.Looper.loop(Looper.java:123)
11-30 20:37:32.870: E/AndroidRuntime(809): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-30 20:37:32.870: E/AndroidRuntime(809): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 20:37:32.870: E/AndroidRuntime(809): at java.lang.reflect.Method.invoke(Method.java:507)
11-30 20:37:32.870: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-30 20:37:32.870: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-30 20:37:32.870: E/AndroidRuntime(809): at dalvik.system.NativeStart.main(Native Method)
11-30 20:37:32.870: E/AndroidRuntime(809): Caused by: java.lang.NullPointerException
11-30 20:37:32.870: E/AndroidRuntime(809): at kfc.project.productdetail.onCreate(productdetail.java:19)
11-30 20:37:32.870: E/AndroidRuntime(809): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-30 20:37:32.870: E/AndroidRuntime(809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-30 20:37:32.870: E/AndroidRuntime(809): ... 11 more
This is the code of the java file
package kfc.project;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class productdetail extends Activity{
#Override
protected void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.productdetail);
Bundle extras = getIntent().getExtras();
String name = extras.getString("name");
if (name != null) {
TextView text1 = (TextView) findViewById(R.id.servingsize);
text1.setText(name);
}
/*
Intent intent = getIntent();
String str = intent.getStringExtra("name");*/
}
}
This is the code for the xml file wherein the error goes comes in when i go to this page. It isnt about cleaning the project and the manifest as well cause it works when i delete the settext code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:scrollbarAlwaysDrawVerticalTrack="true"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="top|center"
android:background="#drawable/bg">
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="right">
<Button
android:id="#+id/button1"
android:background="#drawable/button2"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="ADD TO MENU" android:gravity="right" android:paddingRight="4sp" android:paddingTop="10sp"/>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="left">
</TableRow>
<TableLayout
android:id="#+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Amount Per Serving: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calories:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#android:drawable/divider_horizontal_dark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="2dp"
android:paddingTop="2dp" />
<TableRow
android:id="#+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow14"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="% Daily Value: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="35"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tableLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow9"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Fat:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="60"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Saturated Fat:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="34"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow11"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Trans Fat:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="53"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow12"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cholesterol:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="25"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow13"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sodium:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow15"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Carbohydrate:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow16"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Dietary Fiber:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="34"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow17"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Sugars:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="16"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow18"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Protein:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="40"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#android:drawable/divider_horizontal_dark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="2dp"
android:paddingTop="2dp" />
<TableRow
android:id="#+id/tableRow24"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="#+id/tableRow19"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tableLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow20"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="#+id/tableRow21"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="#+id/tableRow22"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vitamin A: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="40"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Vitamin C: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow23"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calcium: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="35"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Iron: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="40"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow25"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</LinearLayout>
This is where i put the extras
package kfc.project;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class ProductListView extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ProductList pl = new ProductList();
//setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, PRODUCTS));
setListAdapter(new ArrayAdapter<Product>(this, R.layout.list_item, pl.getList() ));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "View Product", Toast.LENGTH_SHORT).show();
Product product = pl.getList().get(position);
Intent intent = new Intent(ProductListView.this, productdetail.class);
intent.putExtra("name",product.getName());
intent.putExtra("calories", product.getCalories());
intent.putExtra("serving size", product.getServingSize());
intent.putExtra("fat", product.getFat());
intent.putExtra("saturated fat", product.getSaturatedFat());
intent.putExtra("trans fat", product.getTransFat());
intent.putExtra("cholesterol", product.getCholesterol());
intent.putExtra("sodium", product.getSodium());
intent.putExtra("carbs", product.getCarbs());
intent.putExtra("protein", product.getProtein());
ProductListView.this.startActivity(intent);
startActivity(new Intent("kfc.project.PRODUCTDETAIL"));
}
});
}
//TEST STRING ARRAY
static String[] PRODUCTS = new String[] {
"Chicken", "Rice", "Coke", "Chicken Burger"
};
}
This is where the array list items are added
package kfc.project;
import java.util.ArrayList;
public class ProductList {
ArrayList<Product> list;
public ProductList (){
list = new ArrayList<Product>();
//CREATE PRODUCT HERE
Product chicken;
list.add(new Product("ChickenTest",99));
Product rice;
list.add(new Product("RiceTest",11));
}
public String[] getNames (){
int c = 0;
int size = list.size() - 1;
String[] names = new String[size];
while (size >= c) {
//names.add(list.get(c).getName());
names[c] = list.get(c).getName();
c++;
}
c = 0;
return names;
}
public ArrayList<Product> getList (){
return list;
}
}
This is the code for the parameteres
package kfc.project;
public class Product {
String name;
int servingSize;
int calories;
int fat;
int saturatedFat;
int transFat;
int cholesterol;
int sodium;
int carbs;
int protein;
public Product (String name, int servingSize, int calories, int fat, int saturatedFat, int transFat, int cholesterol, int sodium, int carbs, int protein){
this.name=name;
this.calories=calories;
this.servingSize=servingSize;
this.fat=fat;
this.saturatedFat=saturatedFat;
this.transFat=transFat;
this.cholesterol=cholesterol;
this.sodium=sodium;
this.carbs=carbs;
this.protein=protein;
}
public Product (String name, int servingSize){
this.name=name;
this.servingSize=servingSize;
}
public String getName() {
return name;
}
public int getServingSize() {
return servingSize;
}
public int getCalories() {
return calories;
}
public int getFat() {
return fat;
}
public int getSaturatedFat() {
return saturatedFat;
}
public int getTransFat() {
return transFat;
}
public int getCholesterol() {
return cholesterol;
}
public int getSodium() {
return sodium;
}
public int getCarbs() {
return carbs;
}
public int getProtein() {
return protein;
}
}
the extras variable is NULL
Bundle extras = getIntent().getExtras();
String name = extras.getString("name");
Update:
First, this line is not needed
startActivity(new Intent("kfc.project.PRODUCTDETAIL"));
Second, you need to getExtra() from getIntent() in productdetail, not getExtras().
For sample:
// get intent object once
Intent receivedIntent = getIntent();
// get product name
String productName = receivedIntent.getExtra("name");
// get serving size
int servingSize = receivedIntent.getExtra("serving size");
// do the same for other props
Bundle extras = getIntent().getExtras();
String name = extras.getString("name");
getExtras() may return null if there are no extras stored in the intent. Check the extras for null.
You might need to do a null check for the extras.
Just remove this line
startActivity(new Intent("kfc.project.PRODUCTDETAIL"));
It starts your activity without extra, causing your extras to be null