ProgressBar片段

时间:2014-11-10 12:09:38

标签: android android-fragments

我在片段中有+-按钮,按下它们我需要更改ProgressBar的进度,但我得到NullPointException

在MainActivity中:

public void selectVentilatorFunction(View view) {

    prg = (ProgressBar)view.findViewById(R.id.ventilator_progress);
    int pressedbutton = view.getId();

    switch (pressedbutton){
     case R.id.increase:
         if(int_ventilator < 100) int_ventilator = int_ventilator + 10;
         break;
     case R.id.decrease:
         if(int_ventilator > 0) int_ventilator = int_ventilator - 10;
         break;
    }
    prg.setProgress(int_ventilator);
    Log.d(TAG,"Ventilator progresas " + int_ventilator);
}

fragment.xml之

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black" >


<RelativeLayout
        android:id="@+id/buttonview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="45dp"
        android:layout_marginLeft="45dp"
        android:layout_marginRight="45dp">

      <ProgressBar
            android:id="@+id/ventilator_progress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="100dp" 
            android:layout_marginTop="200dp"
            android:layout_alignParentTop="true"
            android:progress="50"/>

        <Button
            android:id="@+id/decrease"
            android:layout_width="250dp"
            android:layout_height="160dp"
            android:layout_marginBottom="625dp"
            android:background="@android:color/transparent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:onClick="selectVentilatorFunction" />

        <Button
            android:id="@+id/increase"
            android:layout_width="250dp"
            android:layout_height="160dp"
            android:layout_marginBottom="625dp"
            android:background="@android:color/transparent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:onClick="selectVentilatorFunction" />          

        </RelativeLayout>

        <ImageView        
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/background"
            android:background="@drawable/bg2"
            android:scaleType="fitXY" />
</FrameLayout>

如果需要,可以使用LogCat

11-10 13:53:52.870: E/AndroidRuntime(16452): FATAL EXCEPTION: main
11-10 13:53:52.870: E/AndroidRuntime(16452): Process: com.example.leftpanelmenutest, PID: 16452
11-10 13:53:52.870: E/AndroidRuntime(16452): java.lang.IllegalStateException: Could not execute method of the activity
11-10 13:53:52.870: E/AndroidRuntime(16452):    at android.view.View$1.onClick(View.java:3814)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at android.view.View.performClick(View.java:4424)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at android.view.View$PerformClick.run(View.java:18383)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at android.os.Handler.handleCallback(Handler.java:733)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at android.os.Looper.loop(Looper.java:137)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at android.app.ActivityThread.main(ActivityThread.java:4998)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at java.lang.reflect.Method.invokeNative(Native Method)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at java.lang.reflect.Method.invoke(Method.java:515)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:811)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at dalvik.system.NativeStart.main(Native Method)
11-10 13:53:52.870: E/AndroidRuntime(16452): Caused by: java.lang.reflect.InvocationTargetException
11-10 13:53:52.870: E/AndroidRuntime(16452):    at java.lang.reflect.Method.invokeNative(Native Method)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at java.lang.reflect.Method.invoke(Method.java:515)
11-10 13:53:52.870: E/AndroidRuntime(16452):    at android.view.View$1.onClick(View.java:3809)
11-10 13:53:52.870: E/AndroidRuntime(16452):    ... 11 more
11-10 13:53:52.870: E/AndroidRuntime(16452): Caused by: java.lang.NullPointerException
11-10 13:53:52.870: E/AndroidRuntime(16452):    at com.example.leftpanelmenutest.MainActivity.selectVentilatorFunction(MainActivity.java:76)
11-10 13:53:52.870: E/AndroidRuntime(16452):    ... 14 more

修改

完整代码,其中没有太多内容。在MainActivity中,我有4个按钮并选择片段。

public class MainActivity extends Activity {

    final protected static String TAG = "MainActivity";

    Fragment fr;
    FragmentManager fm;
    FragmentTransaction fragmentTransaction;
    boolean firsttime;
    byte[] myData = {10,20};
    public ProgressBar prg;
    int int_ventilator = 0, int_light = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
              | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
              | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_FULLSCREEN
              | View.SYSTEM_UI_FLAG_IMMERSIVE);
        if(fr==null){
        fr =  new StartMenu();
        fm = getFragmentManager();
        fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
        fragmentTransaction.add(R.id.fragment_place, fr);
        fragmentTransaction.commit();

        }

}

    public byte[] getMyData() {
        return myData;
    }
    public int getInt() {
        return int_ventilator;
    }

    public void selectMainFunction(View view) {

    }

    public void selectVentilatorFunction(View view) {
        int pressedbutton = view.getId();   
        prg = (ProgressBar)view.findViewById(R.id.ventilator_progress); 
        switch (pressedbutton){
         case R.id.increase:
             if(int_ventilator < 100) int_ventilator = int_ventilator + 10;
             break;
         case R.id.decrease:
             if(int_ventilator > 0) int_ventilator = int_ventilator - 10;
             break;
        }
        prg.setProgress(int_ventilator);
        Log.d(TAG,"Ventilator progresas " + int_ventilator);
    }

    public void selectIndoorLightFunction(View view) {
        int pressedbutton = view.getId();
        switch (pressedbutton){
         case R.id.increase:
             if(int_light < 100) int_light = int_light + 10;
             break;
         case R.id.decrease:
             if(int_light > 0) int_light = int_light - 10;
             break;
        }
        Log.d(TAG,"Light progresas " + int_light);
    }

public void selectOutdoorFunction(View view) {

    }

    public void selectMenu(View view) {
         int pressedbutton = view.getId();


         switch (pressedbutton){
         case R.id.startmenu:
             fr = new StartMenu();
             break;
        case R.id.ventilatormenu:
            fr = new VentilatorMenu();   
             break;
        case R.id.indoormenu:
            fr = new IndoorLightMenu();
             break;
        case R.id.outdoormenu:
            fr = new OutdoorLight();
             break;
         }
         fm = getFragmentManager();
         fragmentTransaction = fm.beginTransaction();
         fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
         fragmentTransaction.replace(R.id.fragment_place, fr);
         fragmentTransaction.commit();

    }
}

VentilatorFragment

public class VentilatorMenu extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "VENTILATOR");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        MainActivity activity = (MainActivity) getActivity();
        View view = inflater.inflate(R.layout.bg2layout, container, false);
        return view;
    }

}

2 个答案:

答案 0 :(得分:0)

使用onCreateView()中的布局视图引用初始化ProgressBar,而不是按钮单击视图引用,并且必须在片段而不是活动中实现按钮单击侦听器。

public class VentilatorMenu extends Fragment{

    private ProgressBar prg;
    private Button decrease;
    private Button increase;
    private int int_ventilator;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "VENTILATOR");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bg2layout, container, false);
        prg = (ProgressBar)view.findViewById(R.id.ventilator_progress);
        decrease = (ProgressBar)view.findViewById(R.id.decrease);
        increase = (ProgressBar)view.findViewById(R.id.increase);
        decrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(int_ventilator > 0){
                    int_ventilator = int_ventilator - 10;
                }
                prg.setProgress(int_ventilator);
                Log.d(TAG,"Ventilator progresas " + int_ventilator);
            }
        });
        increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(int_ventilator < 100) {
                    int_ventilator = int_ventilator + 10;
                }
                prg.setProgress(int_ventilator);
                Log.d(TAG,"Ventilator progresas " + int_ventilator);
            }
        });
        return view;
    }

}

答案 1 :(得分:0)

首先,将您的ProgressBar声明为全局变量:

private ProgressBar;

然后在onCreateView()中,执行:

prg = (ProgressBar)view.findViewById(R.id.ventilator_progress);

然后,在selectVentilatorFunction(View view)内:

int pressedbutton = view.getId();

switch (pressedbutton){
 case R.id.increase:
     if(int_ventilator < 100) int_ventilator = int_ventilator + 10;
     break;
 case R.id.decrease:
     if(int_ventilator > 0) int_ventilator = int_ventilator - 10;
     break;
}
prg.setProgress(int_ventilator);
Log.d(TAG,"Ventilator progresas " + int_ventilator);

这将解决您的NPE问题。

关于您的onClick XML回调无法访问您的片段,您可以看到here的解决方案。