1

I am trying to change my text into a hyperlink in a fragment. Sadly, most of the websites only showcase text-hyperlink transformation in the "activity", but not in the "fragment".

So far, when I try to run it, it keeps showing "app has stopped".

  1. I have turned off my instant run to avoid possible error( Session 'app': Error Launching activity), but in vain. And I didn't understand the second method providing in that site(delete .gradle and .idea folder), I don't know where these two folders are, I'm afraid to delete the wrong folder and lose my program...
  2. Some say it's the appcompat-v7 problem, but I don't know how to solve it, I try to import android.app.Activity and discard appcompat-v7, but it resulted in a more complex problem.(because all my codes inside are written in appcompat-v7 form)findViewById() may produce NullPointerException
  3. my hyperlink code is based on these two sites.Creating a hyperlink within a textview inside a fragment and Adding a Hyperlinked text in the array.xml thank you so much in advance for helping me, it means a lot to me><

logcat:

04-16 15:21:22.076 6091-6091/? I/art: Not late-enabling -Xcheck:jni (already on)
04-16 15:21:22.076 6091-6091/? W/art: Unexpected CPU variant for X86 using defaults: x86
04-16 15:21:22.149 6091-6091/com.example.ellen.spinnercontent W/System: ClassLoader referenced unknown path: /data/app/com.example.ellen.spinnercontent-3/lib/x86
04-16 15:21:22.281 6091-6091/com.example.ellen.spinnercontent W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-16 15:21:22.691 6091-6106/com.example.ellen.spinnercontent I/OpenGLRenderer: Initialized EGL, version 1.4
04-16 15:21:22.692 6091-6106/com.example.ellen.spinnercontent D/OpenGLRenderer: Swap behavior 1
04-16 15:21:22.692 6091-6106/com.example.ellen.spinnercontent W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
04-16 15:21:22.692 6091-6106/com.example.ellen.spinnercontent D/OpenGLRenderer: Swap behavior 0
04-16 15:21:22.705 6091-6106/com.example.ellen.spinnercontent D/EGL_emulation: eglCreateContext: 0xa8e851e0: maj 2 min 0 rcv 2
04-16 15:21:22.710 6091-6106/com.example.ellen.spinnercontent D/EGL_emulation: eglMakeCurrent: 0xa8e851e0: ver 2 0 (tinfo 0xa8e837e0)
04-16 15:21:22.790 6091-6106/com.example.ellen.spinnercontent D/EGL_emulation: eglMakeCurrent: 0xa8e851e0: ver 2 0 (tinfo 0xa8e837e0)
04-16 15:21:22.886 6091-6091/com.example.ellen.spinnercontent D/AndroidRuntime: Shutting down VM
04-16 15:21:22.887 6091-6091/com.example.ellen.spinnercontent E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.ellen.spinnercontent, PID: 6091
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                                    at com.example.ellen.spinnercontent.FragmentOne.onCreateView(FragmentOne.java:34)
                                                                                    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
                                                                                    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
                                                                                    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
                                                                                    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
                                                                                    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
                                                                                    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
                                                                                    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
                                                                                    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)

run:

 04/16 14:52:00: Launching app
$ adb push /Users/ellen/Desktop/my-spinner/spinnerContent/app/build/outputs/apk/debug/app-debug.apk /data/local/tmp/com.example.ellen.spinnercontent
$ adb shell pm install -t -r "/data/local/tmp/com.example.ellen.spinnercontent"
Success


$ adb shell am start -n "com.example.ellen.spinnercontent/com.example.ellen.spinnercontent.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet.Waiting for process to come online
Connected to process 2682 on device emulator-5554
Capturing and displaying logcat messages from the application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/System: ClassLoader referenced unknown path: /data/app/com.example.ellen.spinnercontent-3/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xa8e851e0: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xa8e851e0: ver 2 0 (tinfo 0xa8e83820)
D/EGL_emulation: eglMakeCurrent: 0xa8e851e0: ver 2 0 (tinfo 0xa8e83820)
D/AndroidRuntime: Shutting down VM


                  --------- beginning of the crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.ellen.spinnercontent, PID: 2682
                  java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                      at com.example.ellen.spinnercontent.FragmentOne.onCreateView(FragmentOne.java:34)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
                      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
                      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
                      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)
Application terminated.

fragment_fragment_one.xml:

<FrameLayout 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:background="@android:color/holo_orange_light"
    tools:context="com.example.ellen.spinnercontent.FragmentOne">

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/forone"
        android:id="@+id/textView1"
        android:clickable="true"
        android:focusable="true"
        android:layout_gravity="center"/>

</FrameLayout>

FragmentOne.java:

public class FragmentOne extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


       //add this and app keeps stopping 
        TextView tf1 = (TextView) getView().findViewById(R.id.textView1);
        if (tf1 != null) {
            tf1.setMovementMethod(LinkMovementMethod.getInstance());
            String text = "<a href='http://www.youtube.com/watch?v=Fd_AtH0yVqU'> here is the video</a>";
            tf1.setText(Html.fromHtml(text));
        }


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_one, container, false);


    }
}

strings.xml:

<resources>
 <string name="forone">
    <a href="https://www.youtube.com/watch?v=Fd_AtH0yVqU">here is the video </a>
    </string>
</resources>

build.gradle(project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(module)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.ellen.spinnercontent"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:support-v4:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ellen.spinnercontent">

    <uses-permission android:name="android.permission.INTENT"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
ColdFire
  • 6,764
  • 6
  • 35
  • 51
史努比
  • 15
  • 5

5 Answers5

0

You have a NullPointerException because you inflate the layout too late. Do this first in your onCreateView

View rootView = inflater.inflate(R.layout.fragment_fragment_one, container, false);

Clarification:

This line

TextView tf1 = (TextView) getView().findViewById(R.id.textView1);

searches for the TextView, but you haven't defined from which layout so it doesn't know where to look.

Zun
  • 1,553
  • 3
  • 15
  • 26
0

Since

return inflater.inflate(R.layout.fragment_fragment_one, container, false);

creates views needed for your fragment, so the moment you call getView().findViewById(R.id.textView1) there is actually no view to find with.

Instead, you should setup things in onViewCreated(). Make new function in your fragment like:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    TextView tf1 = (TextView) view.findViewById(R.id.textView1);
    if (tf1 != null) {
        tf1.setMovementMethod(LinkMovementMethod.getInstance());
        String text = "<a href='http://www.youtube.com/watch?v=Fd_AtH0yVqU'> here is the video</a>";
        tf1.setText(Html.fromHtml(text));
    }
}

Also read: Creating and Using Fragments

shiftpsh
  • 1,926
  • 17
  • 24
0
        @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {

         View rootView = inflater.inflate(R.layout.fragment_fragment_one, container, false);
         TextView tf1 = (TextView) rootview.findViewById(R.id.textView1);

        return rootview;

}
Jaydeep chatrola
  • 2,423
  • 11
  • 17
0

You need to inflate an layout for FragmentOne where you using getView which returns null view object because there is not view attached.

Update your code with this

View v = inflater.inflate(R.layout.<your_layout>, container, false);

Now use v object to find layout views. For eg to find textview from layout use code as

TextView tv = (TextView) v.findViewById(R.id.tv);

and return v from method onCreateView of fragment replace default return.

Now to convert text to hyper link then in xml add this code

    android:autoLink="email"

"email" to others also based on your requirements.

0

I hope this will work for you.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);
TextView tf1 = (TextView) view.findViewById(R.id.textView1);

if (tf1 != null) {
        tf1.setMovementMethod(LinkMovementMethod.getInstance());
        String text = "<a href='http://www.youtube.com/watch?v=Fd_AtH0yVqU'>here is the video</a>";
        tf1.setText(Html.fromHtml(text));
    }

    return view;
}