0

While trying to manipulate a TextView box, I kept recieving null pointer errors. I have discovered that my TextView box is not linked to my code despite the fact that I have set it to the correct ID. I do not know what the problem is though. I link all my TextViews the same way and this is first time that I have had this problem. This is the part of my XML file that defines the TextView's properties.

<TextView
    android:id="@+id/textviewH"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_alignTop="@+id/textView5"
    android:layout_marginLeft="64dp"
    android:text="@string/HumiumLink"
    />

This is my java file

    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_about_t2m, null);

    Tv = ((TextView) findViewById(R.id.textviewH));
    if (Tv != null) 
    {
        Tv.setMovementMethod(LinkMovementMethod.getInstance());

    }
    dialog.setView(view);
    dialog.setTitle("About T2M");
    dialog.setIcon(R.drawable.ic_launcher);
    dialog.setCancelable(false);
    dialog.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id)
                {
                    dialog.cancel();
                }
            } 
    );

    dialog.create();  
    dialog.show();

I have found that the code inside the if-statement never executes which means that Tv is null. Logcat even shows a strange error that I have not seen before. ![LogCat Error Messages][1] I circled it in blue. This is frustrating.

EDIT1:

This is all for a part of the menu called AboutT2M that is executed from the main activity. The following is the menu code from my main activity

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.AboutMorseID:
            Intent AboutMorseI = new Intent(this, About_Morse.class);
            startActivity(AboutMorseI);
            break;
        case R.id.SettingsID:
            Intent SettingsI = new Intent(this, SettingsActivity.class);
            startActivity(SettingsI);
            break;
        case R.id.ReadMeID:
            Intent ReadMeI = new Intent(this, ReadMe.class);
            startActivity(ReadMeI);
            break;
        case R.id.AboutT2MID:
            Intent AboutT2MI = new Intent(this, About_T2M.class);
            startActivity(AboutT2MI);
            break;
        default:

    }
    return true;
}

[1]: https://i.stack.imgur.com/vRH7Q.pngemphasized text

jizco Borneo
  • 133
  • 1
  • 2
  • 12

2 Answers2

0

Your problem is right here:

View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_about_t2m, null);

You are inflating R.layout.activity_about_t2m. On the next line, you try to initialize Tv:

Tv = ((TextView) findViewById(R.id.textviewH));

Now, it isn't clear if R.id.textviewH is in R.layout.activity_about_t2m or not. If it is, then the line of code above should be changed to:

Tv = ((TextView) view.findViewById(R.id.textviewH));

Because you need the inflated view to find Tv.

If it is not in R.layout.activity_about_t2m, then it should be in R.layout.main_layout_file_name because findViewById() will try to find it inside the layout file that you used with setContentView().

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • R.id.textviewH is in the layout but even with the code changed to Tv = ((TextView) view.findViewById(R.id.textviewH)); I am still seeing the same error in logcat. But I did notice that now Tv is no longer null. – jizco Borneo Aug 04 '13 at 04:06
  • which code are you talking about? The java code is inside an menu activity. The last bit of code is in the main activity. – jizco Borneo Aug 04 '13 at 06:06
  • You are using `this` in `Intent AboutMorseI = new Intent(this, About_Morse.class);`. What does `this` represent? – Vikram Aug 04 '13 at 06:07
  • Why don't you use the code I gave you ? It works ! http://stackoverflow.com/questions/18033479/spannable-string-error/18034429#18034429 – Aleks Aug 04 '13 at 11:58
0

Check the context (getApplicationContext()) of this line....

View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_about_t2m, null);

Try using YourActivity.this instead of getApplicationContext() .

ayon
  • 2,180
  • 2
  • 17
  • 32
  • It compiles and runs. The Textview is no longer null but the application still crashes when the link is clicked. Could the problem be in the way that I defined the link? Here is the logcat output: [LogCat Screen Shot](http://oi42.tinypic.com/2dlq13l.jpg) – jizco Borneo Aug 04 '13 at 04:59
  • It shows Activity not found exception , that means you haven't added the activity (to be started) in your project manifest. Try adding it , it should work. – ayon Aug 04 '13 at 06:27