我正在构建一个循环进度条,其中包含可在github上找到的库。我已经使用给出的xml显示了圆圈,并将依赖项添加到build.gradle中。随着添加正确的attrs和角钱来正确生成它。但是我遇到的问题是java文件应该去的代码是CircularProgressBar circularProgressBar =
(CircularProgressBar)findViewById(R.id.yourCircularProgressbar);
circularProgressBar.setColor(ContextCompat.getColor(this, R.color.progressBarColor));
circularProgressBar.setBackgroundColor(ContextCompat.getColor(this, R.color.backgroundProgressBarColor));
circularProgressBar.setProgressBarWidth(getResources().getDimension(R.dimen.progressBarWidth));
circularProgressBar.setBackgroundProgressBarWidth(getResources().getDimension(R.dimen.backgroundProgressBarWidth));
int animationDuration = 2500; // 2500ms = 2,5s
circularProgressBar.setProgressWithAnimation(65, animationDuration); // Default duration = 1500ms
但是我不知道把它放在哪里。我已经尝试将它放在MainActivity的onCreate中。但那引发了一个错误?
添加了XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:custom="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/hydration">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:layout_marginTop="28dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="59dp"
android:layout_height="60dp"
android:onClick="eight"
android:text="8"
android:id="@+id/button2"
android:layout_marginBottom="53dp"
android:layout_above="@+id/textView"
android:layout_alignRight="@+id/quantity_text_view"
android:layout_alignEnd="@+id/quantity_text_view" />
<Button
android:layout_width="59dp"
android:layout_height="60dp"
android:onClick="thirty_two"
android:text="32"
android:id="@+id/button"
android:layout_above="@+id/button2"
android:layout_alignRight="@+id/quantity_text_view" />
<Button
android:layout_width="59dp"
android:layout_height="60dp"
android:onClick="sixteen"
android:text="16.9"
android:id="@+id/button4"
android:layout_alignTop="@+id/button"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3" />
</LinearLayout>
<TextView
android:text="Total"
android:id="@+id/section_label"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout"
android:layout_centerHorizontal="true" />
<com.mikhaellopez.circularprogressbar.CircularProgressBar
android:layout_width="150dp"
android:layout_height="150dp"
app:cpb_background_progressbar_color="#FFCDD2"
app:cpb_background_progressbar_width="5dp"
app:cpb_progressbar_color="#F44336"
app:cpb_progressbar_width="10dp"
android:id="@+id/circle_progressbar"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true" />
<ProgressBar
style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar2"
android:indeterminate="false"
android:progress="56"
android:layout_above="@+id/editText3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="42dp" />
<ProgressBar
style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:indeterminate="false"
android:progress="78"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="56dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="Daily"
android:layout_above="@+id/progressBar2"
android:layout_alignLeft="@+id/editText3"
android:layout_alignStart="@+id/editText3"
android:layout_marginBottom="28dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:text="Activity"
android:layout_marginBottom="25dp"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:layout_alignBottom="@+id/progressBar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />" />
</RelativeLayout>
已添加的StackTrace
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mikhaellopez.circularprogressbar.CircularProgressBar.setColor(int)' on a null object reference
at com.oxinc.android.hydro.MainActivity$override.Circle(MainActivity.java:32)
at com.oxinc.android.hydro.MainActivity$override.access$dispatch(MainActivity.java)
at com.oxinc.android.hydro.MainActivity.Circle(MainActivity.java:0)
at com.oxinc.android.hydro.MainActivity$override.onCreate(MainActivity.java:83)
at com.oxinc.android.hydro.MainActivity$override.access$dispatch(MainActivity.java)
at com.oxinc.android.hydro.MainActivity.onCreate(MainActivity.java:0)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4098)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5456)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
MainActivity的全部范围
package com.oxinc.android.hydro;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mikhaellopez.circularprogressbar.CircularProgressBar;
import com.oxinc.android.hydro.layout_intializers.Hydration;
import com.oxinc.android.hydro.layout_intializers.Tips;
import com.oxinc.android.hydro.layout_intializers.Info;
public class MainActivity extends AppCompatActivity {
public void Circle() {
CircularProgressBar circularProgressBar = (CircularProgressBar)findViewById(R.id.circle_progressbar);
circularProgressBar.setColor(ContextCompat.getColor(this, R.color.cpb_progressbar_color));
circularProgressBar.setBackgroundColor(ContextCompat.getColor(this, R.color.cpb_background_progressbar_color));
circularProgressBar.setProgressBarWidth(getResources().getDimension(R.dimen.cpb_progressbar_width));
circularProgressBar.setBackgroundProgressBarWidth(getResources().getDimension(R.dimen.cpb_background_progressbar_width));
int animationDuration = 2500; // 2500ms = 2,5s
circularProgressBar.setProgressWithAnimation(65, animationDuration); // Default duration = 1500ms
}
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Circle();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
//Displays the sectionsm to the View
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Hydration";
case 1:
return "Info";
case 2:
return "Tips";
}
return null;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Hydration();
case 1:
return new Info();
case 2:
return new Tips();
default:
// this should never happen
return null;
//return new Fragment();
}
}
}
//Executes the methods to increment by amount
public int quantity = 0;
public String outcome = "";
public int required = 0;
public int height = 0;
public void eight(View view) {
quantity = quantity + 8;
displayQuantity(quantity);
}
public void twelve(View view) {
quantity = quantity + 12;
displayQuantity(quantity);
}
public void sixteen(View view) {
quantity = quantity + 16;
displayQuantity(quantity);
}
public void thirty_two(View view) {
quantity = quantity + 32;
displayQuantity(quantity);
}
public void sixty_four(View view) {
quantity = quantity + 64;
displayQuantity(quantity);
}
//Reset Button
public void reset(View view) {
quantity = 0;
displayQuantity(quantity);
outcome = "";
// displayOutcome(outcome);
}
//Displays Quantity to the TextView
public void displayQuantity(int quantity) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText(quantity + "");
}
}
答案 0 :(得分:0)
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Hydration();
case 1:
return new Info();
case 2:
return new Tips();
default:
// this should never happen
return null;
//return new Fragment();
}
}
此部分为您的标签创建片段。每个片段都有它自己的布局,你说hydration.xml实际上是你在那里出现的XML。
为了能够在Fragment的视图中引用视图,您需要在Fragment的onCreateView方法中进行操作。否则,当您尝试引用它时,视图不在屏幕上。
所以,这就是我猜测应该在Hydration Fragment中的内容:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.hydration, parent, false);
//Find your stuff from the inflated rootView of your Fragment
CircularProgressBar progressBar = rootView.findViewById(R.id.circle_progressbar);
//Do stuff with progressBar
return rootView;
}