Java Android应用程序打开后立即关闭

Java Android应用程序打开后立即关闭,java,android,git,runtime-error,Java,Android,Git,Runtime Error,在设备上安装应用程序后,应用程序在打开后立即关闭 我正在通过Udacity学习android。在第三课中,他们给了我一个带有 最小布局。[Udacity代码][1]。因此,他们要求开发以适配器视图格式显示震级位置和日期的完整代码 Studio没有显示任何错误,但如果我将光标放在getMagnitude()方法上,它会显示方法调用getMagnitude可能会产生“java.lang.NullPointerException”,而对于setAdapter,它也显示相同的内容,我的完整代码如下所示

在设备上安装应用程序后,应用程序在打开后立即关闭

我正在通过Udacity学习android。在第三课中,他们给了我一个带有 最小布局。[Udacity代码][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);
    }
}
EarthquakeAdapter.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
package com.example.android.quakereport;
导入android.content.Context;
导入android.support.annotation.NonNull;
导入android.support.v7.view.menu.ListMenuItemView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入java.util.List;
/**
*由raghavendra于2017年3月7日创建。
*/
公共类地震适配器扩展阵列适配器{
公共地震适配器(上下文,列出地震){
超级(上下文,0,地震);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//检查现有视图是否正在重新使用,否则会膨胀视图
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutFlater.from(getContext()).inflate(
R.layou.u活动,父项,假);
}
地震电流地震=getItem(位置);
TextView magnitudeView=(TextView)listItemView.findViewById(R.id.magnitude);
震级视图.setText(current地震.getMagnite());
TextView locationView=(TextView)listItemView.findViewById(R.id.location);
locationView.setText(currentSe震.getLocation());
TextView dateView=(TextView)listItemView.findViewById(R.id.date);
dateView.setText(currentSevent.getDate());
返回listItemView;
}
}
[1]: https://github.com/udacity/ud843-QuakeReport

地震类没有默认构造函数。因此,mMagnitude可以为null,您应该将其设置为

private String mMagnitude = "";
或者为地震类添加默认构造函数:

public Earthquake() {
    mMagnitude = "";
    ...
}

我认为问题在于你的地震.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;
    }




}
尝试


请大家找出问题所在。请发布错误日志。您上传的代码中没有GetMagnite。这只是本课程的起点。请推送您的更改。您的变量名称应为大写。公共地震(弦震级、弦位置、弦日期){mMagnitude=震级;mLocation=位置;mDate=日期;}@GustavoConde camelCase是个人喜好的问题。它不会以任何方式影响应用程序的性能。
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;
    }
}