2017-05-09 127 views
-1

我編程,我的應用程序使用的ListView是我創造我的addapter類來擴展ArrayAddapter並創建我的getter類來獲得我的話到我要膨脹的觀點,但是當我運行應用程序它停止。應用程序崩潰 - 我的Android應用程序

我addapter類代碼

package charpman.com.quakereport; 

import android.content.Context; 
import android.graphics.Typeface; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 

/** 
* Created by DCharpMan on 4/19/2017. 
* <p> 
* class to populate each list view item 
*/ 

public class QuakeAdapter extends ArrayAdapter<earthquakeClass> { 


    /** 
    * Constructor 
    * 
    * @param context   The current context. 
    * @param earthquakeClass the array to be populated 
    */ 
    public QuakeAdapter(@NonNull Context context, ArrayList<earthquakeClass> earthquakeClass) { 
     super(context, R.layout.description_house, earthquakeClass); 
    } 


    @NonNull 
    @Override 
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

     View Customize = convertView; 
     if (Customize == null) { 
      Customize = LayoutInflater.from(getContext()).inflate(R.layout.description_house, parent, false); 
     } 
     earthquakeClass earthquakeClass = getItem(position); 

     TextView textView = (TextView) Customize.findViewById(R.id.text_magnitude); 
     textView.setText(earthquakeClass.getMagnitude()); 
     textView.setTypeface(Typeface.MONOSPACE, 2 + 1); 


     TextView textView1 = (TextView) Customize.findViewById(R.id.text_loctaion); 
     textView1.setText(earthquakeClass.getLocation()); 
     textView1.setTypeface(Typeface.SERIF, 1); 

     TextView textView2 = (TextView) Customize.findViewById(R.id.text_daye); 
     textView2.setText(earthquakeClass.getDate()); 
     textView2.setTypeface(Typeface.DEFAULT_BOLD, 3); 


     return Customize; 

    } 

} 

我mainActivity.java代碼

package charpman.com.quakereport; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ListView; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 

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


     final ArrayList<earthquakeClass> country = new ArrayList<earthquakeClass>(); 

     country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017")); 
     country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017")); 
     country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017")); 
     country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017")); 
     country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017")); 


     ListView listView = (ListView) findViewById(R.id.list_view); 

     QuakeAdapter quakeAdapter = new QuakeAdapter(this, country); 


     listView.setAdapter(quakeAdapter); 

    } 
} 

我消氣類代碼

package charpman.com.quakereport; 

/** 
* Created by CharpMan on 4/19/2017. 
* class to get the earthQuake details 
* details includes Magnitude,location and date of earthquake 
*/ 

public class earthquakeClass { 

    // globale variables that holds the earthquake information 
    String magnitude; 
    String location; 
    String date; 

    //contructor for the class takes in three params 
    // @param Magnitude tell the magnitude of the quake 
    // @params Location tells the location where the quake occured 
    // @params date tells the day of the quake 
    public earthquakeClass(String Magnitude, String Location, String Date){ 
     magnitude = Magnitude; 
     location = Location; 
     date = Date; 

    } 

    // create public methods that returns each information 

    // method to the the magnitude 
    public String getMagnitude(){ 
     return magnitude; 
    } 

    // methid to return the loctaion 
    public String getLocation(){ 
     return location; 
    } 

    // methid to return the day of quake occurence 

    public String getDate(){ 
     return date; 
    } 



} 

我MainActivity.xml代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="charpman.com.quakereport.MainActivity"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:background="@android:color/darker_gray" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingLeft="10dp" 
      android:text="Magnitude" 
      android:textAllCaps="true" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center_horizontal" 
      android:paddingLeft="10dp" 
      android:text="Location" 
      android:textAllCaps="true" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center_horizontal" 
      android:paddingLeft="20dp" 
      android:text="date" 
      android:textAllCaps="true" /> 

    </LinearLayout> 


    <ListView 
     android:background="@android:color/secondary_text_light" 
     android:id="@+id/list_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </ListView> 



</LinearLayout> 

爲我在Addapter類的GetView方法膨脹佈局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" 
    android:padding="10dp"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 


     <TextView 
      android:id="@+id/text_magnitude" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@android:color/white" 
      android:paddingLeft="14dp" 
      android:text="7.8" /> 


     <TextView 
      android:id="@+id/text_loctaion" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@android:color/white" 
      android:gravity="center" 
      android:paddingLeft="5dp" 
      android:text="Nigeria" 
      android:typeface="serif" /> 


     <TextView 
      android:id="@+id/text_daye" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1" 
      android:background="@android:color/white" 
      android:paddingLeft="25dp" 
      android:text="28,02,2017" /> 


    </LinearLayout> 


</LinearLayout> 

我包括我的logcat的截圖enter image description here

+1

發表您的活動代碼 –

+1

發表您的XML以及 –

+1

發表您的Java方法和XML文件以供參考! – aB9

回答

0

我試圖通過代碼來讀取和我注意到,離子我的ListView代碼我加了標籤化背景

機器人:背景=「@機器人:顏色/ secondary_text_light」

我應該有限定/ PR中複製的顏色代碼oject colors.xml並使用它。由於

0

按照錯誤日誌,我認爲在XML文件中有一些按照您的適配器或也許你正在使用您的適配器的對象,它是不存在於你的XML文件,該文件是不兼容的。