我创建了一个2x2网格,其中前3个网格有三个(Thingspeak)图表从GitHub导入(https://github.com/MacroYau/ThingSpeakAndroid)and第四个网格将在稍后实现,所以暂时我只是添加了一个空白图表。
我已经检查了stackoverflow上的所有类似问题,并阅读了他们的答案,我已经理解了第60行log {(chartView = (LineChartView) findViewById(R.id.chart);
)中的错误,这意味着这个id指向null
,但我没有'知道如何解决它,chart
在id
布局中作为activity_main.xml
出现。
我使用了以下文件:activity_main.xml
,main.xml
,TempHumidity.java
和GridViewAdapter.java
来自TempHumidity.java
public class TempHumidity extends AppCompatActivity {
private ThingSpeakChannel tsChannel, tsChannel1, tsChannel2;
private ThingSpeakLineChart tsChart, tsChart1, tsChart2;
private LineChartView chartView, chartView1, chartView2;
GridView simpleGrid;
int charts[] = {R.id.chart, R.id.chart1, R.id.chart2};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
simpleGrid = (GridView) findViewById(R.id.simpleGridView); // init GridView
// Create an object of CustomAdapter and set Adapter to GirdView
GridViewAdapter customAdapter = new GridViewAdapter(getApplicationContext(), charts);
simpleGrid.setAdapter(customAdapter);
// Connect to ThinkSpeak Channels
tsChannel = new ThingSpeakChannel(377467);
tsChannel1 = new ThingSpeakChannel(357670);
tsChannel2 = new ThingSpeakChannel(377509);
// Set listener for Channel feed update events
tsChannel.setChannelFeedUpdateListener(new ThingSpeakChannel.ChannelFeedUpdateListener() {
@Override
public void onChannelFeedUpdated(long channelId, String channelName, ChannelFeed channelFeed) {
// Show Channel ID and name on the Action Bar
getSupportActionBar().setTitle(channelName);
getSupportActionBar().setSubtitle("Channel " + channelId);
// Notify last update time of the Channel feed through a Toast message
Date lastUpdate = channelFeed.getChannel().getUpdatedAt();
Toast.makeText(TempHumidity.this, lastUpdate.toString(), Toast.LENGTH_LONG).show();
}
});
// Fetch the specific Channel feed
tsChannel.loadChannelFeed();
// Create a Calendar object dated 1 minutes ago
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, -1);
// Configure LineChartView
chartView = (LineChartView) findViewById(R.id.chart); //Logcat error points here
chartView.setZoomEnabled(false);
chartView.setValueSelectionEnabled(true);
GridViewAdapter.java
public class GridViewAdapter extends BaseAdapter {
private Context context;
private final int[] charts;
LayoutInflater inflter;
public GridViewAdapter(Context Appcontext, int[] charts) {
this.context = Appcontext;
this.charts = charts;
inflter = (LayoutInflater.from(Appcontext));
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_main, null); // inflate the layout
LineChartView cv = (LineChartView) view.findViewById(R.id.chart); // get the reference of ImageView
return view;
}
@Override
public int getCount() {
return charts.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--
-->
<GridView
android:id="@+id/simpleGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:padding="1dp"
android:numColumns="2" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Container LinearLayout -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Row 1 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<lecho.lib.hellocharts.view.LineChartView
android:id="@+id/chart"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<lecho.lib.hellocharts.view.LineChartView
android:id="@+id/chart1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
<!-- Row 2 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<lecho.lib.hellocharts.view.LineChartView
android:id="@+id/chart2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<lecho.lib.hellocharts.view.LineChartView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
Logcat
:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.macroyau.thingspeakandroid.demo/com.macroyau.thingspeakandroid.demo.TempHumidity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.macroyau.thingspeakandroid.demo.TempHumidity.onCreate(TempHumidity.java:60)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
01-14 17:11:28.007 23142-23146/com.macroyau.thingspeakandroid.demo
D/dalvikvm: GC_CONCURRENT freed 587K, 19% free 8887K/10932K, paused 4ms+22ms, total 114ms
当我在GridView
中添加两个相似的图表时运行代码(就像在GitHub中一样):
目前我的activity_main.xml
布局如下所示:
答案 0 :(得分:0)
正如Mike M.在评论中指出chart
文件中没有main.xml
id,这就是它指向null的原因。
我所要做的就是删除main.xml
文件并在activity_main.xml
方法中将布局设置为onCreate
,如下所示:
setContentView(R.layout.activity_main)
(因为我已在其中制作了2x2布局activity_main.xml
)
并删除GridViewAdapter.java
。