I tried to create a simple color change application. In my application I have created a fragment which has linear layout and inside which text view is present. I am trying to change the color of textview in my application in a regular interval. In order to change color in regular interval, I have used random() function to create a random number between 0 and 255 and use those numbers as a parameter to create random colors. I am again using those random colors to set the background of my textview which is present in linear layout.
Below are my codes:
MainActivity.java
package com.example.abcd.color_change;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragment = getSupportFragmentManager();
Fragment existingFragment = fragment.findFragmentById(R.id.container);
fragment.beginTransaction().replace(R.id.linear1, existingFragment).commit();
}
}
ColorChangeFragment.java
package com.example.abcd.color_change;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by abcd on 10/7/17.
*/
public class ColorChangeFragment {
private TextView mTextView;
private Random rand = new Random();
private int randNum = 0;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment,container, false);
mTextView = (TextView) v.findViewById(R.id.textview);
Timer timer = new Timer();
//MyTimer mt = new MyTimer();
/*timer.schedule(mt, 1000, 1000);*/
timer.schedule(new TimerTask(){
@Override
public void run() {
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
int randomColor = Color.rgb(r,g,b);
mTextView.setBackgroundColor(randomColor);
}
},5000,5000);
return v;
}
/* class MyTimer extends TimerTask{
@Override
public void run() {
runOnUiThread(new Runnable(){
@Override
public void run() {
Random rand = new Random();
mTextView.setBackgroundColor(Color.argb(255, rand.nextInt(256), rand.nextInt(256) ));
}
});
}
}*/
}
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id ="@+id/linear1"
android:background="@android:color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id = "@+id/textview"/>
</LinearLayout>
Below is the error message I receive upon building the code.
10-30 12:56:21.393 21574-21574/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abcd.color_change, PID: 21574
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abcd.color_change/com.example.abcd.color_change.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2762)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2848)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6334)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:441)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:432)
at com.example.abcd.color_change.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:6743)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2848)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6334)
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)
10-30 12:56:21.393 21574-21574/? D/AppTracker: App Event: crash
If you're having a single Fragment, you don't need to use replace()
. You just need to use add(). The following code is wrong:
FragmentManager fragment = getSupportFragmentManager();
Fragment existingFragment = fragment.findFragmentById(R.id.container);
fragment.beginTransaction().replace(R.id.linear1, existingFragment).commit();
Because R.id.container
is not a fragment but a FrameLayout
. There is no fragment in your activity layout but only a FrameLayout which is:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Where it should be:
<LinearLayout>
<fragment android:name="com.example.ColorChangeFragment"
android:id="@+id/color_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
To solve the problem, you need to change the code for adding the fragment with:
Fragment newFragment = new ColorChangeFragment();
FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container, fragment);
fragmentTransaction.commit();
Then, you need to fix the ColorChangeFragment
class to extend the android.support.v4.app.Fragment
. So, change your class to something like this:
...
import android.support.v4.app.Fragment;
public class ColorChangeFragment extends Fragment {
...
}
R.id.container
is not an id of a Fragment
and calling findFragmentById(R.id.container);
will return null.
To add a fragment, use the following code:
One method:
Fragment newFragment = new ColorChangeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the container view with this fragment,
// and add the transaction to the back stack
transaction.replace (R.id.container , newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit ();
Fragment Transaction tutorial: https://developer.android.com/guide/components/fragments.html#Transactions
Another method:
You can also use fragment directly from the XML of your activity.
<FrameLayout>
<fragment
android:name="com.example.abcd.color_change.ColorChangeFragment"
...
/>
</FrameLayout/>
Then you don't have to use code related to FragmentManager
Check the tutorial here: https://developer.android.com/training/basics/fragments/creating.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.