0

我有在一個適配器視圖中顯示MPAndroidChart如果任何一個可以協助,我使用的,因爲在一個佈局使用不同視圖的一個獨立的適配器空對象引用,MPAndroidChart空對象全球化志願服務青年

這裏是適配器:

public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.ViewHolder> { 

private Activity activity; 

BarChart chart ; 
ArrayList<BarEntry> BARENTRY = new ArrayList<BarEntry>(); 
ArrayList<String> BarEntryLabels = new ArrayList<String>(); ; 
BarDataSet Bardataset ; 
BarData BARDATA ; 

public VerticalListAdapter(Activity activity) { 
    this.activity = activity; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    LayoutInflater inflater = activity.getLayoutInflater(); 
    View view = inflater.inflate(R.layout.item_recycler_view, parent, false); 
    BARENTRY = new ArrayList<BarEntry>(); 
    BarEntryLabels = new ArrayList<String>(); 

    AddValuesToBARENTRY(); 
    AddValuesToBarEntryLabels(); 

    Bardataset = new BarDataSet(BARENTRY, "Projects"); 

    BARDATA = new BarData(BarEntryLabels, Bardataset); 

    Bardataset.setColors(ColorTemplate.COLORFUL_COLORS); 
    chart.setData(BARDATA); 
    chart.animateY(3000); 
    return new ViewHolder(view); 
} 

private void AddValuesToBarEntryLabels() { 

    BarEntryLabels.add("January"); 
    BarEntryLabels.add("February"); 
    BarEntryLabels.add("March"); 
    BarEntryLabels.add("April"); 
    BarEntryLabels.add("May"); 
    BarEntryLabels.add("June"); 

} 

private void AddValuesToBARENTRY() { 
    BARENTRY.add(new BarEntry(2f, 0)); 
    BARENTRY.add(new BarEntry(4f, 1)); 
    BARENTRY.add(new BarEntry(6f, 2)); 
    BARENTRY.add(new BarEntry(8f, 3)); 
    BARENTRY.add(new BarEntry(7f, 4)); 
    BARENTRY.add(new BarEntry(3f, 5)); 

} 


@Override 
public void onBindViewHolder(ViewHolder viewHolder, final int position) { 
    viewHolder.BarChart.setData(BARDATA); 

} 

@Override 
public int getItemCount() { 
    return 1; 
} 

/** 
* View holder to display each RecylerView item 
*/ 
protected class ViewHolder extends RecyclerView.ViewHolder { 

    BarChart BarChart; 

    public ViewHolder(View view) { 
     super(view); 
     BarChart = (BarChart) view.findViewById(R.id.chart1); 

    } 

} 

}

和XML

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:layout_marginBottom="5dp" 
android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp" 
android:layout_marginTop="5dp"> 

    <com.github.mikephil.charting.charts.BarChart 
     android:id="@+id/chart1" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" /> 

這裏是logcat的:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.BarChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference 
                       at info.devexchanges.recyclerview.VerticalListAdapter.onCreateViewHolder(VerticalListAdapter.java:32) 
                       at info.devexchanges.recyclerview.VerticalListAdapter.onCreateViewHolder(VerticalListAdapter.java:18) 
                       at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5482) 
                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707) 
                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) 
                       at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) 
                       at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) 
                       at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) 
                       at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) 
                       at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) 
                       at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2625) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                       at android.view.View.measure(View.java:18788) 
                       at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1283) 
                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                       at android.widget.ScrollView.onMeasure(ScrollView.java:340) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                       at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 
                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 
                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 
                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 
                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 
                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                       at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                       at android.view.Choreographer.doFrame(Choreographer.java:606) 
                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                       at android.os.Handler.handleCallback(Handler.java:739) 
                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
+0

什麼是文件f的名稱rom哪些XML代碼段被共享? – kujeensiti

+0

item_recycler_view.xml – Mohtashim

+0

In Log Cat chart.setData(BARDATA);在BARDATA = new BarData(BarEntryLabels,Bardataset)中顯示數據時始終爲null; <<<< – Mohtashim

回答

0

我必須解決這個問題,並想後的代碼,我在空引用,因爲我沒有在onCreateViewHolder開始佈局,添加該兩行後,我的佈局示出了卡內部視圖圖形視圖(圖被使用在列表視圖中具有動態視圖的類型)在onCreateViewHolder

BarChart chart; 
chart = (BarChart) view.findViewById(R.id.chart1);