2016-02-10 4 views
0

У меня есть следующие активности main.xml файлане удалось найти mpandroidchart с помощью findViewById

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.bright.myapplication.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" 
     app:backgroundTint="#088A08" 
     /> 

</android.support.design.widget.CoordinatorLayout> 

content.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.bright.myapplication.MainActivity" 
    tools:showIn="@layout/activity_main" 
    android:id="@+id/mylayout" 
    > 


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

     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="62dp" 

     android:layout_alignParentEnd="true"> 

    </com.github.mikephil.charting.charts.BarChart> 
</RelativeLayout> 

В MainActivity.java упоминается ниже

MainActivity.java

public class MainActivity extends AppCompatActivity {  
    private RelativeLayout myLayout; 
      @Override protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 


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

      BarData data = new BarData(getXAxisValues(), getDataSet()); 
      chart.setData(data); 
      chart.setDescription("My Chart"); 
      chart.animateXY(2000, 2000); 
      chart.invalidate(); 
      setContentView(chart);  
     } 
} 

Когда я использую findViewById (R.id.chart), диаграмма инициализируется как null. поэтому я не могу использовать диаграмму. любезно помогите мне исправить это.

+0

Проверьте ** ** setContentView сделать его ниже super.onCreate и установить ** mainlayout ** xml file like (R.Layout.mainlayout) – SaravanaRaja

ответ

4

Проблема в том, что вы вызываете setContentView() после инициализации. У вас есть два варианта. Первая из них - инициализировать BarChart как, а затем вызвать ее в параметре setContentView() в качестве параметра, а второй - установить макет как параметр в setContentView() и ниже инициализировать BarChart.

Например

Первый

@Override  
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     BarChart chart = this; 

     BarData data = new BarData(getXAxisValues(), getDataSet()); 
     chart.setData(data); 
     chart.setDescription("My Chart"); 
     chart.animateXY(2000, 2000); 
     chart.invalidate(); 
     setContentView(chart); 
    } 

Второй

 @Override  
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(layout.xml); 

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

      BarData data = new BarData(getXAxisValues(), getDataSet()); 
      chart.setData(data); 
      chart.setDescription("My Chart"); 
      chart.animateXY(2000, 2000); 
      chart.invalidate(); 
    } 
+0

Спасибо, vspallas. Я попробовал второй вариант, о котором упомянул u v. Он работал без проблем –

+0

рад, что это помогло :) –

Смежные вопросы