2017-03-07 10 views
-2

デバイスにアプリをインストールした後、アプリを開くとすぐにアプリが終了します。Androidアプリは開封直後に終了します

私はUdacityを通じてAndroidを学習しています。レッスン3では、彼らは私に 最小レイアウトのアンドロイドコードを与えました。[Udacity code] [1]。そこで、マグニチュードの位置と日付をアダプタビュー形式で表示する完全なコードを開発するように指示しました。

Studioは、すべてのエラーを表示されていないが、私はgetMagnitude()方法それは、メソッド呼び出しを示しているgetMagnitudeにカーソルを置く場合は、「java.lang.NullPointerExceptionが」を生成することができ、setAdapterのためにも、それは同じことを示していますそして私の完全なコードは

EarthquakeActivity.java 




/*`` 
* Copyright (C) 2016 The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
package com.example.android.quakereport; 
import java.lang.String; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import java.util.ArrayList; 

public class EarthquakeActivity extends AppCompatActivity { 

    public static final String LOG_TAG = EarthquakeActivity.class.getName(); 

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

     // Create a fake list of earthquake locations. 
     ArrayList<Earthquake> earthquakes = new ArrayList<>(); 
     earthquakes.add(new Earthquake("7.2","San Francisco", "Feb 2,2017 ")); 
     earthquakes.add(new Earthquake("5.2","London", "Oct 2,2017 ")); 
     earthquakes.add(new Earthquake("9.2","Tokyo", "Feb 2,2017 ")); 
     earthquakes.add(new Earthquake("8.2","Mexico City", "Mar 2,2017 ")); 
     earthquakes.add(new Earthquake("7.2","Mascow", "Feb 16,2017 ")); 
     earthquakes.add(new Earthquake("6.2","Rio de Jeneiro", "Feb 28,2017 ")); 
     earthquakes.add(new Earthquake("1.2","India", "Jan 2,2017 ")); 


     // Find a reference to the {@link ListView} in the layout 
     ListView earthquakeListView = (ListView) findViewById(R.id.list); 

     // Create a new {@link ArrayAdapter} of earthquakes 
    EarthquakeAdapter adapter = new EarthquakeAdapter(this ,earthquakes); 

     // Set the adapter on the {@link ListView} 
     // so the list can be populated in the user interface 
     earthquakeListView.setAdapter(adapter); 
    } 
} 

聞くEarthquake.java

package com.example.android.quakereport; 

/** 
* Created by raghavendra on 3/7/2017. 
*/ 

public class Earthquake { 


    private String mMagnitude; 

    private String mLocation; 

    private String mDate; 


    public Earthquake(String Magnitude, String Location, String Date) 
    { 
     mMagnitude = Magnitude; 

     mLocation = Location; 

     mDate = Date; 

    } 

    public String getMagnitude() 
    { 

     return mMagnitude; 
    } 

    public String getLocation() 
    { 

     return mLocation; 
    } 

    public String getDate() 
    { 

     return mDate; 
    } 




} 

EでありますarthquakeAdapter.java

package com.example.android.quakereport; 

import android.content.Context; 
import android.support.annotation.NonNull; 
import android.support.v7.view.menu.ListMenuItemView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.List; 

/** 
* Created by raghavendra on 3/7/2017. 
*/ 

public class EarthquakeAdapter extends ArrayAdapter<Earthquake> { 

    public EarthquakeAdapter(Context context, List<Earthquake> earthquakes) { 
     super(context, 0, earthquakes); 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Check if the existing view is being reused, otherwise inflate the view 
     View listItemView = convertView; 
     if (listItemView == null) { 
      listItemView = LayoutInflater.from(getContext()).inflate(
        R.layout.earthquake_activity, parent, false); 
     } 

     Earthquake currentEarthquake = getItem(position); 



     TextView magnitudeView = (TextView) listItemView.findViewById(R.id.magnitude); 

     magnitudeView.setText(currentEarthquake.getMagnitude()); 

     TextView locationView = (TextView) listItemView.findViewById(R.id.location); 

     locationView.setText(currentEarthquake.getLocation()); 

     TextView dateView = (TextView) listItemView.findViewById(R.id.date); 

     dateView.setText(currentEarthquake.getDate()); 






return listItemView; 



    } 

} 








    [1]: https://github.com/udacity/ud843-QuakeReport 
+0

plz誰でも問題を見つけてください。 –

+3

エラーログを投稿してください –

+1

アップロードしたコードにはgetMagnitudeはありません。これは、コースの出発点に過ぎません。変更をプッシュしてください。 –

答えて

0

を私はこの問題は、あなたのEarthquake.java

であると思います試してみる。

public class Earthquake { 


    private String mMagnitude; 

    private String mLocation; 

    private String mDate; 


    public Earthquake(String Magnitude, String Location, String Date) 
    { 
     this.mMagnitude = Magnitude; 

     this.mLocation = Location; 

     this.mDate = Date; 

    } 

    public String getMagnitude() 
    { 

     return mMagnitude; 
    } 

    public String getLocation() 
    { 

     return mLocation; 
    } 

    public String getDate() 
    { 

     return mDate; 
    } 
} 
0

Earthquakeクラスにはデフォルトコンストラクタがありません。したがってmMagnitudeはnullになる可能性があり、あなたが

private String mMagnitude = ""; 

や地震クラスのデフォルトコンストラクタを追加し、それを設定する必要があります。

public Earthquake() { 
    mMagnitude = ""; 
    ... 
} 
関連する問題