My app crashes because I declared a checkbox equal to a checkbox - java

When I open this activity my app crashes and says that it is due to this line of code:
private CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly);
The layout for alarm_details_repeat_weekly is right here:
<CheckBox
android:id="#+id/alarm_details_repeat_weekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/alarm_details_label_wednesday"
android:layout_alignBottom="#+id/alarm_details_label_wednesday"
android:layout_alignParentRight="true"
android:checked="true" />
Since some people asked, here is the logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2083)
at minutemovement.mintemovement.com.minutemovement.AlarmDetailsActivity.<init>(AlarmDetailsActivity.java:28)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)
Here is the full code: http://pastebin.com/EygRhAj4

You have a typo. You declared your id as alarm_details_repeat_wednesday in your xml but then tried to get an object with id alarm_details_repeat_weekly. Change your code to this:
private CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_wednesday);

Try remove the private,
CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly); or declare the private CheckBox outside oncreate and initialize the object this way: chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly);

Related

Android setText problem error with editText [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I'm trying to set a textview to a editText text. When I do that I have this error:
Process: com.example.game, PID: 9829
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.game.MainActivity.onClick(MainActivity.java:79)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
It's strange because when I do system.out.println with the text I get
I/System.out: Player1
Can someone help me ?
My code:
System.out.println(p1.getText().toString());
System.out.println(p1.getEditableText().toString());
j1.setText(p1.getEditableText().toString());
j2.setText(p2.getEditableText().toString());
p1 and p2 are editText and j1 and j2 are TextView. I don't know if it's the problem but they are in different resource layout.
All your views are needed to be initialized after setting setContentView(R.layout.activity_main)
However in your case you have them in different resource layout.
let's say you want to have views of layout_xyz in activity_main.
in activity_main
<include
android:id="#+id/xyzView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_xyz" />
In MainActivity.java
initialize views from layout_xyz
View xyzView = findViewById(R.id.xyzView);
TextView j1FromXyz = xyzView.findViewById(R.id.j1);
TextView j2FromXyz = xyzView.findViewById(R.id.j2);
For more: Re-using layouts with <include/>

Android EditTextPreference custom dialog: Add functionality to button

I am trying to add a preference which will store a top level path so that they app can auto-discover some files in that directory on the phone. I have added an EditTextPreference and changed the dialog of it so that it has a button as well as a text box.
<EditTextPreference android:id="#+id/etpTopLevelAutoDiscoverPath"
android:key="settings_top_level_AutoDiscover_path"
android:summary=""
android:title="Location to search for custom configurations"
android:dialogLayout="#layout/preference_browse_to_path"
android:defaultValue=""/>
I am struggling however gaining access to the button to add a click listener to it. I think my main problem is that I can't instantiate the custom dialogs view because I don't know when the dialog for EditTextPreference is displayed.
This is how I am attempting to set the get the button object but obviously when it gets to creating the button it crashes because the view doesn't exist.
View browseToPathView = this.findViewById(R.id.browseToPathView);
Button browse = (Button)browseToPathView.findViewById(R.id.btnRootBrowse);
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dtg.cinet/com.dtg.cinet.PreferencesScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
I am fairly new to android and have come into a project midway so it is entirely possible that I am just not doing it in the right way but I am fairly stuck at the moment and can't figure out what to do.

Unable to properly reference TextView from java within same activity

I am running a basic application that passes information between two screens, but I'm crashing as soon as I select to move to the second activity.
Commenting out blocks of code, I have narrowed the problem down to a NULL object.
XML File "activity_two"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.id11434343.exercise2.ActivityTwo">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/hintName"
android:id="#+id/textViewName"
android:layout_below="#+id/textViewHeader"
android:layout_marginTop="15dp"/>
Java File "ActivityTwo"
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent parentIntent = getIntent();
String name = parentIntent.getStringExtra(ActivityOne.NAME_MESSAGE);
TextView finalName = (TextView) findViewById(R.id.textViewName);
finalName.setText(name);
}
When I run TextView finalName = (TextView) findViewById(R.id.textViewName);, it passes a null reference. I have a similar line in the first activity, EditText name = (EditText) findViewById(R.id.editTextName);, but experience no problem.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference com.id11434343.exercise2.ActivityTwo.onCreate ActivityTwo.java:34)"
I receive this error, and have tried debug to get a closer look. My EditText name = null, however, the R.id.editTextName = 2131558488, which appears to be a valid ID, in line with the IDs from other elements. I have found a couple of similar questions, on here, but they previously appear to have come down to simple errors, such as using incorrect references, so I double checked my variables, and compared it directly to the working version, to ensure I was not missing something small.
Am I missing something here? I can't understand why I'm having troubles with a ViewText, where I didn't have any problems doing the same thing with an EditText.
This passes null when you use a wrong layout in setContentView() means that the given id doesn't exist in the given layout.

java.lang.IllegalStateException: Could not find a method after Parse implementation

I've read everywhere - couldn't find a solution.
My project was working fine, I stated the methods I wanted to be called to the onClick inside the XML file like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:theme="#style/AppTheme"
android:id="#+id/actualMainActivity"
tools:context=".ActualMainActivity"
><Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add new noob"
android:id="#+id/addNewNoob"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:onClick="addNewNoob"
android:layout_alignTop="#+id/listOfNoobs"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /></RelativeLayout>
the method inside my java class (ActualMainActivity) was like this:
public void addNewNoob(View view) {
Log.i("Tag", "you clicked the add button");
}
Only after I've implemented the Parse SDK to work with, my Gradle resynced and now when I run the application I get the 'Method not found exception'.
I would like to point out that when I assign an onClickListener directly to the button like this:
final Button a = (Button) findViewById(R.id.addNewNoob);
a.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Log.i("Tag", "you clicked the add button");
}
});
that works perfectly fine...
What have I changed when I installed the SDK ?
How can I get back to the way that I write the methods on XML and they are executed accordingly.
Really confused... thanks in advance
here's the logcat:
02-09 11:29:34.917 2669-2669/com.example.shaked.sqliteexample02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.shaked.sqliteexample02, PID: 2669
java.lang.IllegalStateException: Could not find a method listOfNoobs(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'listOfNoobs'
at android.view.View$1.onClick(View.java:3994)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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)
Caused by: java.lang.NoSuchMethodException: listOfNoobs [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:3987)
         
Thanks in advance
Create a New Project move the old Classes to the New one and Check!
its giving methodexception error
so see to it that the method named listofnoobs() is not missing.
also clean your project and then rebuild it

Telling the Difference between two Android GUI objects with the same ID

Say we have two XML files, of which these are snippets:
firstxml.xml
<?xml version="1.0" encoding="utf-8"?>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button1 />
secondxml.xml
<?xml version="1.0" encoding="utf-8"?>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button1 />
In our onCreate() method:
//Assume all packages necessary are imported
public class Example
{
public void onCreate(Bundle sis)
{
super.onCreate(sis);
setContentView(R.layout.firstxml);
Button thisButton = (Button) findViewbyId(R.id.button1);
}
}
Which button is called and instantiated when this code runs? Since the two buttons are in two different files, will the button in firstxml.xml be called because it is the content view of the function?
Thanks in advance for your answers!
When you set the firstxml to be the content view, that layout gets inflated. findViewById will find the id's in the current layout that's inflated rather than on every layout.
setContentView method description kind of answers your question.
You are referencing the the first XML layout file R.layout.firstxml in the activity's onCreate(Bundle...) method, so the search here will be made.
Any call to findViewById(int id); will search in your inflated layout R.layout.firstxml.
The R.java file is automatically generated when you are defining/adding a new view to your layout.
You can use the same id multiple times, but not in the same layout !
The code will call and instantiate the button1 set on your firstxml.xml file.
Note For future readers. If the layout file doesnot contain button1 it will throw null pointer exception and the editor wont know that there is an error. This is because R.id.button1 is static field in your R.java class. Also provide better naming convention to your variables for maintainability.
Which button is called and instantiated when this code runs?
Since you used the firstxml.xml as the layout of the activity you are calling the reference of that button from that xml not the button from the secondxml.xml.
And in your R.java it will only generate one instance of that id.
sample:
public static final class id {
public static final int button1=0x7f090009;
}
so when you used that reference of that button it will first find/check the layout's id if it exist if it does not exist then it will throw NPE.

Categories