簡體   English   中英

GridView中的java.lang.NullPointerException錯誤導致應用程序崩潰

[英]java.lang.NullPointerException error in GridView making app crash

我創建了一個2x2網格,其中前3個網格具有從GitHub( https://github.com/MacroYau/ThingSpeakAndroid )導入的三個(Thingspeak)圖表,第四個網格將在以后實現,因此暫時我只添加了一個空白圖表。 我已經檢查了關於stackoverflow的所有類似問題並閱讀了它們的答案,並且我了解了logcat在第60行的錯誤( chartView = (LineChartView) findViewById(R.id.chart); )意味着該id指向null但是我不知道如何解決它,因為chart在我的activity_main.xml布局中以id出現。

我使用了以下文件: activity_main.xmlmain.xmlTempHumidity.javaGridViewAdapter.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

當我運行代碼(就像在GitHub中那樣)而未在GridView添加兩個相似的圖表時: 在此處輸入圖片說明

目前,我的activity_main.xml布局如下所示:

在此處輸入圖片說明

正如Mike M.在評論中指出的那樣, chart ID在main.xml文件中不存在,這就是它指向null的原因。 我要做的就是刪除main.xml文件,然后在onCreate方法中將布局設置為activity_main.xml ,如下所示: setContentView(R.layout.activity_main) (因為我已經在其中創建了2x2的布局activity_main.xml )並刪除GridViewAdapter.java

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM