0

我对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();
      }
 }



}

有人知道我做错了什么吗?谢谢你。

4

2 回答 2

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

在你的类声明中,这样你就可以使用关键字this来引用监听器。

于 2013-03-01T21:13:35.300 回答
1

您的 Activity 需要实现接口OnItemSelectedListener

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

完成此操作后,您可以将 set listener 行更改为:

 spinner.setOnItemSelectedListener(this);
于 2013-03-01T21:14:40.900 回答