2013-03-01 71 views
0

我對android非常陌生,所以不要責怪我的錯誤,如果你可以嘗試幫助我。 我的應用程序開始正常,但只要我嘗試切換到另一個活動,它就表示我的應用程序「不幸停住」。在那個活動中,我有一個微調器,我想從中讀取所選項目並將這些信息存儲在我的數據庫中。 這裏是日誌文本:Android:無法加載活動

03-01 21:54:01.392: E/Trace(625): error opening trace file: No such file or directory (2) 
03-01 21:54:01.882: D/dalvikvm(625): GC_FOR_ALLOC freed 62K, 4% free 7991K/8259K, paused 75ms, total 78ms 
03-01 21:54:01.892: I/dalvikvm-heap(625): Grow heap (frag case) to 9.317MB for 1536016-byte allocation 
03-01 21:54:01.982: D/dalvikvm(625): GC_CONCURRENT freed <1K, 4% free 9491K/9799K, paused 33ms+6ms, total 92ms 
03-01 21:54:02.533: D/gralloc_goldfish(625): Emulator without GPU emulation detected. 
03-01 21:54:03.062: D/dalvikvm(625): GC_CONCURRENT freed 12K, 2% free 9866K/9991K, paused 5ms+26ms, total 224ms 
03-01 21:54:06.322: D/AndroidRuntime(625): Shutting down VM 
03-01 21:54:06.322: W/dalvikvm(625): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
03-01 21:54:06.352: E/AndroidRuntime(625): FATAL EXCEPTION: main 
03-01 21:54:06.352: E/AndroidRuntime(625): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s_home/com.example.s_home.Osvetlenie}: java.lang.ClassCastException: com.example.s_home.Osvetlenie cannot be cast to android.widget.AdapterView$OnItemSelectedListener 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.app.ActivityThread.access $600(ActivityThread.java:130) 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.os.Looper.loop(Looper.java:137) 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.app.ActivityThread.main(ActivityThread.java:4745) 
03-01 21:54:06.352: E/AndroidRuntime(625): at java.lang.reflect.Method.invokeNative(Native Method) 
03-01 21:54:06.352: E/AndroidRuntime(625): at java.lang.reflect.Method.invoke(Method.java:511) 
03-01 21:54:06.352: E/AndroidRuntime(625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
03-01 21:54:06.352: E/AndroidRuntime(625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-01 21:54:06.352: E/AndroidRuntime(625): at dalvik.system.NativeStart.main(Native Method) 
03-01 21:54:06.352: E/AndroidRuntime(625): Caused by: java.lang.ClassCastException: com.example.s_home.Osvetlenie cannot be cast to android.widget.AdapterView$OnItemSelectedListener 
03-01 21:54:06.352: E/AndroidRuntime(625): at com.example.s_home.Osvetlenie.onCreate(Osvetlenie.java:30) 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.app.Activity.performCreate(Activity.java:5008) 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
03-01 21:54:06.352: E/AndroidRuntime(625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
03-01 21:54:06.352: E/AndroidRuntime(625): ... 11 more 
03-01 21:54:14.143: I/Process(625): Sending signal. PID: 625 SIG: 9 

這裏是一個崩潰的活動:

package com.example.s_home; 

import com.example.s_home.DBAdapter; 

import android.os.Bundle; 
import android.annotation.SuppressLint; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.support.v4.app.NavUtils; 
import android.widget.AdapterView; 
import android.widget.Adapter; 

public class Osvetlenie extends Activity { 

@TargetApi(11) 
@SuppressLint("NewApi") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_osvetlenie); 

    Spinner spinner = (Spinner) findViewById(R.id.house_spinner); 
    spinner.setOnItemSelectedListener((OnItemSelectedListener) this); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource (this,R.array.house_arrays, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_osvetlenie, menu); 
    return true; 
} 


public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 

    } 
    return super.onOptionsItemSelected(item); 
} 

public class SpinnerActivity extends Activity implements OnItemSelectedListener { 

    String miest; 
    public void onItemSelected(AdapterView<?> parent, View view, 
      int pos, long id) { 

      miest=parent.getItemAtPosition(pos).toString(); 
    } 

    public void onNothingSelected(AdapterView<?> parent) { 
     // Another interface callback 
    } 

    public void osvZap(View view) { 


     DBAdapter db = new DBAdapter(this); 
     db.open(); 

     long id; 
      id = db.insertTitle(
       "cas", 
       "PI", 
       miest, "ON");   
     System.out.println(miest); 
      db.close(); 
     } 
} 



} 

任何人有一個想法,我做了什麼錯?謝謝。

回答

2

你logcat的狀態:

03-01 21:54:06.352: E/AndroidRuntime(625): Caused by: java.lang.ClassCastException: com.example.s_home.Osvetlenie cannot be cast to android.widget.AdapterView$OnItemSelectedListener

您的代碼有:

spinner.setOnItemSelectedListener((OnItemSelectedListener) this); 

this指Osvetlenie這是你的活動。你不能將它轉換爲監聽器。如果你想使用this,你需要推動接口。

implements OnItemSelectedListener 
在類declartion

,所以你可以使用關鍵字this指的監聽器。

+0

謝謝veryyyyyy muchhhhh :) – 2013-03-01 21:30:03

+0

當你用答案解決了問題,你應該檢查左邊最能幫助你標記答案的檢查。 – wtsang02 2013-03-01 21:33:59

1

你的活動需要實現該接口OnItemSelectedListener

public class Osvetlenie extends Activity implements OnItemSelectedListener { 
    // Your code 
} 

一旦你做到了這一點,你可以更改設置監聽線路:

spinner.setOnItemSelectedListener(this); 
+0

謝謝veryyyyyy muchhhhh :) – 2013-03-01 21:29:14