NullPointerException in my aboutButton I think - android

My second book for Android programming Hello, Android by Ed Burnette. I'm using eclipse. The code matches the book and it matches the code downloaded from the website of the book. But I know I'm doing something wrong here. I added a bunch of breakpoints where I figure (mostly guessing) where the problem might be happening. What I've come to is that this line of code is the culprit (SudokuActivity.java line 21) You can download the entire code here
http://kbsoftware.dlinkddns.com/Sudoku.zip
aboutButton.setOnClickListener(this);
but I just can't figure out why ? It must be the result of something I'm doing wrong somewhere else. I've deleted and recreated the avd and that made no difference so not it. I'm at a lost here.
public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
}
I want to thank everyone who responded, it's all fixed and working and I could not have done it without your help. I've learned more working on this problem then I would of in weeks if not months of problem free programming.

Yout aboutButton is not getting bound properly.
Do something like
Button aboutButton = (Button) findViewById(R.id.about_button);

I downloaded your code and it runs correctly on my phone. So if your code is the same it should run.
It seems that findViewById didn't find the view and then calling a method on a null object caused the nullpointerexception.
My dumb question: have you tried a project clean up? You can even try saving your classes, deleting the project and creating a new one.
Hope it helps

Related

Android Studio Kotlin - Onclick listener not being recognized

So this is my first time using Kotlin and Android studio and I really got to say thus far my experience has been horrible. No other language or IDE has put me in the position where I had to do more than hours research to try and make a simple button load the next page and then still being unable to do it.
This is my Login.kt page
and there you can see its having problems with the findviewbyID and setonlclicklistener.
Top of my Activity_login.xml page
The button im trying to create the onclick event for.
Please keep in mind I did try just the normal creation of setonclick for the button but then I didnt seem to recognize that I have a button with the ID of login_btn
change the init button to
val button=findViewById<Button>(R.id.login_btn)
in my case i have removed onclick from xml.
val button : Button = findViewById(R.id.login_btn);
button.setOnClickListener {
}
If no answers above can help you, you can use viewbinding to access your view. I had so many problems while using findViewById as a newbie. Here is the documentation,
https://developer.android.com/topic/libraries/view-binding

I can't see my logs in Android Studio logcat

Here's a simple app, I'm trying to create logs in the printToLogs method.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("Log0","test");
}
public void printToLogs(View view){
TextView menuTextView1 = findViewById(R.id.menu_item_1);
TextView menuTextView2 = findViewById(R.id.menu_item_2);
TextView menuTextView3 = findViewById(R.id.menu_item_3);
String item1 = (String) menuTextView1.getText();
String item2 = (String) menuTextView2.getText();
String item3 = (String) menuTextView3.getText();
Log.v("Log1",item1);
Log.v("Log2",item2);
Log.v("Log3",item3);
}
but logs with the tags Log1, Log2, Log3 are not shown at all in the logcat, what does show up is the Log0 in the onCreate method, but the other ones in printToLogs never show up when I search for them at all. I attempted re-installing the app and restarting logging. That didn't work.
The menu items are: Mango sorbet, Blueberry pie, Chocolate lava cake. And yes, I tried searching for them, and they are not in the logcat either.
If this is your actual code, you aren't even calling printToLogs in the onCreate method. You should be more diligent before posting something simple like this.
Barring a serious runtime environment issue, this problem should be fairly easy to solve.
It seems as if the printToLogs(View view) method is to be executed in response to the user clicking a button. If so, try including the following line in your activity_main.xml if you haven't already:
android:onClick="printToLogs"
This will bind the button on the UI with the printToLogs(View view) method.
If, on the other hand, printToLogs(View view) is intended to be a standalone method (i.e. one that should execute regardless of user input) it should not accept a View as an argument. For your purposes, its parameter list should be completely empty. In other words, the method signature should read:
public void printToLogs()
Also, it should be called within the onCreate(Bundle savedInstances) method. Add the following to the onCreate(Bundle savedInstances) method:
printToLogs();
This will initiate the execution of the method as soon as the app begins to run.
Make sure the logcat filter is set to "Verbose" when testing like so: (img is link since apparently I need 10 rep. to embed images into answers directly)
logcat filter
heyy add your method/function name in your button by using property section or just android:onClick in your xml file and then it will be solved

Got exception in create run time editText object

I attempt to create an EditText object at run time this way:
EditText et=new EditText(MyActivity.this);
And i have got below exception in Samsung galaxy tab in other tablet it works fine.
android.os.Handler.<init>(Handler.java:121)
android.sec.clipboard.ClipboardExManager$1.<init>(ClipboardExManager.java:90)
android.sec.clipboard.ClipboardExManager.<init>(ClipboardExManager.java:89)
android.app.ContextImpl$8.createService(ContextImpl.java:292)
android.app.ContextImpl$ServiceFetcher.getService(ContextImpl.java:199)
android.app.ContextImpl.getSystemService(ContextImpl.java:1158)
android.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:79)
android.app.Activity.getSystemService(Activity.java:3932)
android.widget.EditText.<init>(EditText.java:68)
android.widget.EditText.<init>(EditText.java:62)
android.widget.EditText.<init>(EditText.java:58)
com.example.myProject.MyActivity.insertTextBox(MyActivity.java:8249)
com.example.myProject.MyActivity$19.run(com.example.myProject.MyActivity.java:8057)
I have solve this issue just by adding one view from xml layout which visually gone and than add all view runtime in linearlayout and its works fine. I don't know what's the issue but it's solve.
You should really post more code for the problem to become apparent.
Based on the little information you provided my guess is you tried to create that EditText before the context is initialized.
Try to move that code in the onCreate() method if it's not already there.

"Force Close" Error on declaring TextView and ToggleButton

Well basicly I have a textview and when the application is created it sets a string as the textviews text not hard, but I get a force close error when I run the app on my phone.
TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);
Then I have a error on a togglebutton also
ToggleButton silent=(ToggleButton)findViewById(R.id.silentbutton);
silent.setChecked(false);
And I have errors for all my other buttons/textviews can anyone help, please?!
EDIT:
I cant post pics because I am a new member, :(
Link to imgshack http://imageshack.us/photo/my-images/849/unledggp.png/
If code for the whole textview snippet.
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_UNMOUNTED)) {
TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);
}
OnCreate Function
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkSD();
checkRing();
checkWifi();
checkBt();
}
Look for all instances of sd_textview and make sure the one that you're trying to reference is a TextView. If you want more clarity you can debug your code and see what object is actually being returned by not casting into a TextView:
View sdcard = findViewById(R.id.sd_textview); //debug this
//you can also log the View object to see the type
Log.d("Test", "" + sdcard);
Looking at your error log (assuming its the right error log) you have a ClassCastException in the checkWifi method. Edit your question and include ALL of the onCreate method and all of the checkWifi method, but I expect you are using the same id for multiple views.
Two things I can think of (although seeing more code would help).
Make sure you have called setContentView(R.layout.main) (or whatever your layout file is called). Do this BEFORE any attempt to use findViewById(...).
Secondly sdcard.setText(R.string.not_mounted); in this statement R.string.not_mounted is a resource ID (int) and not a string. You would need to use...
sdcard.setText(getString(R.string.not_mounted));

XML scope in Android

Is there anywhere that I can find documentation on the scope of the XML files? I have an app I am currently working on and have been struggling with getting a feature to work and it seems that the problem I am having is that I am trying to access an element in an XML file that must be out of scope. To simplify the layout, my project has main.xml, sub.xml, main.java, and sub.java files in it. As you can probably guess, main.java works with main.xml and sub.java is working with the elements in sub.xml. Here's where the issue comes in, I have a TextView element that is created in main.xml that I would like to modify the text in, but the action that would trigger it will occur in sub.java. I can't figure out how to change it from sub.java, and I can't figure out how to move the element into sub.xml. The code I am using is pretty simple:
TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);
I get a FC every time I run the app, but if I move the code into main.java, it runs flawlessly. If anyone can offer any ideas, or point me in the direction of some documentation that would explain what java files can access what elements in which xml files, that would be awesome! Sorry for the novel, but I'm just struggling to get the point across. Thanks.
try like this Bryan in Main.xml file it works with no issue...........Declare first & then Initialize it...
public class Main extends Activity {
static TextView tv;
static Button submit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
tv = (TextView) findViewById(R.id.header_text1);
}
}
Activity.findViewById(int) only works if that view is in the activity's layout. So no, you can't refer to a view in main.xml because that layout doesn't apply to sub.
Do you have any TextViews in sub.xml called myTitle?
You can access the the textview of main.java(main.xml) in submain.java as follows
In main.java write the following code
static TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);
and u can access this submain.java as
Main.titleText.setText(filePath);

Categories

Resources