0

I have a PreferenceScreen and then dynamically create a PreferenceCategory, and on this category I set the layout newCategory.setLayoutResource(<PackageName>.R.Layout.myLayout);. The layout is a couple of TextView's and a button. When I try to change the text of these TextViews TextView tvTextView = (TextView) findViewById(<PackageName>.R.id.tvTextView); tvTextView.setText("Test"); I get java.lang.NullPointerException. I have no idea what the hell is going on, and I am pulling my hair out, it must be something very simple I am not getting.

Log Cat: Hope this is the right stuff I am meant to copy =S

12-16 23:25:23.450: E/AndroidRuntime(5412): FATAL EXCEPTION: main
12-16 23:25:23.450: E/AndroidRuntime(5412): java.lang.NullPointerException
12-16 23:25:23.450: E/AndroidRuntime(5412):     at <PackageName>.GpsLocation$2.onClick(GpsLocation.java:190)
12-16 23:25:23.450: E/AndroidRuntime(5412):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-16 23:25:23.450: E/AndroidRuntime(5412):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 23:25:23.450: E/AndroidRuntime(5412):     at android.os.Looper.loop(Looper.java:144)
12-16 23:25:23.450: E/AndroidRuntime(5412):     at android.app.ActivityThread.main(ActivityThread.java:4937)
12-16 23:25:23.450: E/AndroidRuntime(5412):     at java.lang.reflect.Method.invokeNative(Native Method)
12-16 23:25:23.450: E/AndroidRuntime(5412):     at java.lang.reflect.Method.invoke(Method.java:521)
12-16 23:25:23.450: E/AndroidRuntime(5412):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-16 23:25:23.450: E/AndroidRuntime(5412):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-16 23:25:23.450: E/AndroidRuntime(5412):     at dalvik.system.NativeStart.main(Native Method)

The line 190 has tvTextView.setText("Test");, that's where the error is happening apparently.

user959631
  • 1,004
  • 2
  • 14
  • 34
  • 2
    Post your LogCat (stack trace). Also, why are you using the "long" version (`.R....`) just using R should be fine. – A--C Dec 16 '12 at 23:20
  • That is because when I use just (R.id.Views), in the drop-down of id's, when you are typing R.id and then press "." my Views aren't there =S, but the "long" version has the Views. – user959631 Dec 16 '12 at 23:23
  • Are you accidentally importing a different R file? – Sam Dec 16 '12 at 23:27
  • Did I forget to mention I am a total noob, lol, how..well..errm..how do I ...errmm...get the stack trace? =O – user959631 Dec 16 '12 at 23:27
  • @Sam, Yeah I am using Eclipse, – user959631 Dec 16 '12 at 23:28
  • @user959631 highlight the red text that appears when your app crashes and copy it (ctrl + c). – A--C Dec 16 '12 at 23:30
  • 1
    Back to `.R` check your `import` statements, you most likely have `import android.R;` which is very bad. But Eclipse sometimes imports this for you by mistake... – Sam Dec 16 '12 at 23:34
  • Yeah, when I use `R.id.View`, it underlines in Red and hovering over it gives me an option to import `android.R`, so I have no option, this happened before, so I started the project all over again -.- and it's happened again, but I ain't going to re-do the project again. hehe – user959631 Dec 16 '12 at 23:36
  • 1
    According to the LogCat, the problem is in `onClick()`. Please post this method to your question and indicate which line is 190. – Sam Dec 16 '12 at 23:36
  • 1
    Don't start the project over again... Eclipse typically imports `android.R` when there is an error in one (or more) of your XML files. Use the **Problems** window to help locate the error. – Sam Dec 16 '12 at 23:38
  • Ahhh, well I have an `AlertDialog` and there is an `onClickListener` there, btw is there a way to get line numbers on the side, like in Visual Studio? – user959631 Dec 16 '12 at 23:38
  • Well, I know the R file "disappears" when there is something wrong in your project, but what if there isn't any problems and the R file is still there? That's what happened to me, weird? – user959631 Dec 16 '12 at 23:40
  • 1
    You can use Ctrl+L to jump to a line. "but what if there isn't any problems and the R file is still there?" I have never seen this happen, XML errors can be _very_ sneaky. – Sam Dec 16 '12 at 23:42
  • Eerrrm, well sometimes when there is something wrong in the project, sometimes with the Resources, the R file "disappears", the only problems I have is that I am "hard-coding" the text of a TextView or a Button or something like that. Could it possibly be because of that? – user959631 Dec 16 '12 at 23:44
  • Yeah, `TextView tvTextView = (TextView) findViewById(.R.id.tvTextView); tvTextView.setText("Test");` is what I use to change text, this textview is in the layout that I apply to the PreferenceCategory. – user959631 Dec 16 '12 at 23:51
  • 1
    I believe these problems are linked. If you solve why your R file cannot be built, then `findViewById()` should work with a regular `R.id.tvTextView` (assuming it is already visible.) But there can be a lot of reasons for: [R cannot be resolved - Android error](http://stackoverflow.com/q/885009/1267661). – Sam Dec 16 '12 at 23:52
  • Errm, I have a "hunch" that the problem is because I am trying to change the text of this textview, I have a TextView on a layout, and I apply this layout to a PreferenceCategory, so when I try to change the text of the textview it errors. By the way, my Class extends PreferenceActivity. But when I try to change text of a textview when the class extends Activity (not PreferenceActivity) I am able to do so. – user959631 Dec 16 '12 at 23:55
  • 1
    @Sam Ok, I got the R thing fixed, I just removed all the `.R` and replaced with just `R`, and removed the `import android.R` and it works, so thanks for that Sam. But my app is still crashing at that same location =S – user959631 Dec 17 '12 at 00:10
  • 2
    Hurray! (Almost...) Is `R.id.tvTextView` in the layout that you pass to `setContentView()`? – Sam Dec 17 '12 at 00:15
  • Noo, I have a PreferenceActivity, and in the `OnCreate()` I have `addPreferencesFromResource(R.xml.Prefs);` Do you think it's because it's a preference screen? – user959631 Dec 17 '12 at 00:17
  • 1
    Since you are never specifying a layout for your PreferenceActivity, your textView will be null - it technically does not exist for that Activity. – A--C Dec 17 '12 at 00:25
  • So does `newCategory.setLayoutResource(R.layout.myLayout);` not count? Or do I also have to say in the `OnCreate()` `setContentView();`? – user959631 Dec 17 '12 at 00:28
  • Try `getPreferenceScreen().findViewById()` or `newCategory.findViewById()`. – Sam Dec 17 '12 at 00:32
  • @Sam I'm looking through the Preference API, no mention of a `findViewById()`, only getView. Seems like a pointless omission since getView needs View arguments. – A--C Dec 17 '12 at 00:35
  • Yeah, I just had a look and that won't really work, lol, I am making it clear to you guys right? or more info needed? If so, I can give.. =] hehe – user959631 Dec 17 '12 at 00:36

1 Answers1

0

Thanks guys for helping out with speedy replies, but I figured out a different approach. Thanks for your time.

user959631
  • 1,004
  • 2
  • 14
  • 34