2015-02-04 4 views
-3

텍스트 파일의 요소를 읽은 다음이를 ListView으로 표시해야하는 새 활동을 만들었지 만 에뮬레이터에서 해당 아이콘을 클릭하여 활동을 시작하려고하면이 오류가 발생합니다 메시지 :활동이 시작되지 않습니다

불행하게도, 앱이 프로젝트가 제대로 컴파일 된 것으로 보이기 때문에

내가 왜 이해가 안 작동이 중지되었습니다?

추신 : 열려고하는 활동은 SortedLocationsListActivity입니다.

로그 캣 오류 메시지 :

다음
02-04 13:51:32.340 2900-2900/fr.isima.android.tp1.tp1 E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: fr.isima.android.tp1.tp1, PID: 2900 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.isima.android.tp1.tp1/fr.isima.android.tp1.tp2.SortedLocationsListActivity}: java.lang.NumberFormatException: Invalid long: "1400390852000A" 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NumberFormatException: Invalid long: "1400390852000A" 
      at java.lang.Long.invalidLong(Long.java:124) 
      at java.lang.Long.parse(Long.java:366) 
      at java.lang.Long.parseLong(Long.java:353) 
      at fr.isima.android.tp1.tp2.SortedLocationsListActivity.onCreate(SortedLocationsListActivity.java:37) 
      at android.app.Activity.performCreate(Activity.java:5933) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

내 SortedLocationsListActivity의 일부 (I 수정 한에서 onCreate 방법) :

public class SortedLocationsListActivity extends ListActivity { 

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


     InputStream is = getResources().openRawResource(R.raw.locations); 
     BufferedReader in = new BufferedReader(new InputStreamReader(is)); 
     List<String> maListe = new ArrayList<String>(); 
     String myligne; 
     LocationAdapter adapter = new LocationAdapter(this, R.layout.row_location); 

     try { 
      while((myligne = in.readLine()) != null) 
      { 
       String a[] = myligne.split(";"); 
       Long _date=Long.parseLong(a[2], 36); 
       System.out.println(a[0]+" "+a[1]+" "+a[2]); 
       adapter.addLocation(a[0],a[1],_date); 
      } 
      setListAdapter(adapter); 

     } 
     catch(IOException e) 
     { 
      System.out.println("Error"); 
     } 


    } 

이 좋아, 그래서 문제가 발생 코드 :

Long _date=Long.parseLong(a[2], 36); 

기본적으로 시도하고 싶습니다. 예를 들어 "1400390852000A"일 수있는 String을 Long 유형으로 변환하려면 어떻게해야합니까?

public class LocationAdapter extends BaseAdapter { 
 

 
     private List<Location> Locations; 
 
     int monLayout; 
 
     LayoutInflater inflater; 
 

 

 
     public LocationAdapter(Context context, int layout){ 
 
      monLayout=layout; 
 
      inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
      Locations = new ArrayList<Location>(); 
 
     } 
 

 

 

 
     private class Location{ 
 
      public String name; 
 
      public String address; 
 
      public Long date; 
 

 
      public Location(String _name,String _address , Long _date){ 
 
       name=_name; 
 
       address=_address; 
 
       date=_date; 
 

 
      } 
 
     } 
 

 

 
     private class ViewHolder{ 
 

 
      TextView name_view; 
 
      TextView address_view; 
 
      TextView date_view; 
 

 
      public ViewHolder(View rowLayout){ 
 

 
       name_view = (TextView)rowLayout.findViewById(R.id.name); 
 
       date_view = (TextView)rowLayout.findViewById(R.id.date); 
 
       address_view = (TextView)rowLayout.findViewById(R.id.address); 
 
      } 
 
     } 
 

 
     public void addLocation(String _name,String _address,Long _date){ 
 
      //Création d'une nouvelle location avec les données en paramètres 
 
      Location new_loc = new Location(_name, _address,_date); 
 

 
      //Ajout de la location à la liste des locations 
 
      Locations.add(new_loc); 
 

 
     } 
 
    /*Méthodes de la classe mère*/ 
 

 
     @Override 
 
     public int getCount() { 
 
      return 0; 
 
     } 
 

 
     @Override 
 
     public Object getItem(int position) { 
 
      return null; 
 
     } 
 

 
     @Override 
 
     public long getItemId(int position) { 
 
      return 0; 
 
     } 
 

 
     @Override 
 
     public View getView(int position, View convertView, ViewGroup parent) 
 
     { 
 
      View view = convertView; 
 

 
      if (view == null) 
 
       view = inflater.inflate(monLayout, parent, false); 
 

 
      ViewHolder holder = (ViewHolder)view.getTag(); 
 
      if (holder == null) 
 
      { 
 
       holder = new ViewHolder(view); 
 
       view.setTag(holder); 
 
      } 
 

 
      Location location = (Location)getItem(position); 
 

 
      holder.name_view.setText(location.name); 
 
      holder.address_view.setText(location.address); 
 
      holder.date_view.setText("Test"); 
 

 
      return view; 
 
     } 
 
}

:

좋아, 내가 오류를 수정하고 내가 활동을 시작하는 관리했지만 그것은 단지 대신 내가 원하는 목록의 빈 화면을 표시, 여기에 내가 코드 어댑터입니다

누군가가 문제의 출처를 말해 줄 수 있습니까?

+1

는 unhandlled 예외를 호출합니다. DDMS windows Logcat보기에서 로그를보고 여기에 게시하십시오. 매니페스트에서 활동을 신고하셨습니까? – AAnkit

+2

어떻게하면 누군가가 여러분의 응용 프로그램 코드없이 여러분을 도울 수 있다고 기대할 수 있습니까? 'Stack Trace'또는 더 잘 알려진 'Logcat' –

+0

일부 코드 또는 스택 추적 게시, 우리가 무슨 일이 일어나는지 잘 모르는 경우에는 도움을 줄 수 없습니다. 뒤에서. –

답변

1

이유는이 >> java.lang.NumberFormatException의이다 : 잘못된 길이 : "1400390852000A"

당신은 당신의 활동에 Long.parseLong에 의해 길이 긴 문자열 형식으로 변환하려고합니다. 이것을 정정하면 효과가 있습니다.

문자열 값 >> 1400390852000A는 Long으로 변환 할 수 없습니다.

그것을 죽일 :

0

이동을 SortedLocationsListActivity 클래스의 37 라인과 긴 구문 분석 방법을 확인하려면 SortedLocationsListActivity 라인 수 (37) 버그가 확인 이동합니다. 이 매개 변수가 항상 숫자인지 확인하십시오.

Long _date=Long.parseLong(a[2], 36); 

문자열 myligne은 숫자 뒤에 숫자 만 사용하십시오.

기본적으로 롱 타입으로 "1400390852000A"가 될 수있는 문자열을 변환하는 것이 좋습니다. 어떻게 올바르게 할 수 있습니까?

문자열의 형식에 따라 다릅니다.항상 문자가 마지막에있는 경우라면 당신은 구문 분석하기 전에 코드에서이 문제를 추가하여 수를 얻을 수 있습니다 :

String a[] = myligne.split(";"); 
String myFinalString = a[2].substring(0, a[2].length()-1); 
Long _date=Long.parseLong(myFinalString, 36); 
0

은 그것의 37

SortedLocationsListActivity 라인을 살펴 모양이 NumberFormatException과 같습니다. 길이가 잘못되었습니다 : "1400390852000A".

체크 여기 매개 변수 :

Long _date=Long.parseLong(a[2], 36); 
관련 문제