gpt4 book ai didi

android - fragment 中的进度条

转载 作者:太空狗 更新时间:2023-10-29 15:01:00 26 4
gpt4 key购买 nike

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

在主 Activity 中:

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 个按钮并选择 fragment 。

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();

}
}

呼吸机 fragment

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;
}

}

最佳答案

在 onCreateView() 中使用您的布局 View 引用而不是按钮单击 View 引用来初始化 ProgressBar,并且必须在 fragment 而不是 Activity 中实现按钮单击监听器。

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;
}

}

关于android - fragment 中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26843512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com