0

私はアンドロイドに非常に慣れていないので、私の間違いについて私を責めないでください、そしてあなたが私を助けてくれるなら。アプリケーションは正常に起動しますが、別のアクティビティに切り替えようとすると、アプリが「残念ながら停止しました」と表示されます。そのアクティビティでは、選択したアイテムを読み取り、この情報をデータベースに保存するスピナーがあります。ログテキストは次のとおりです。

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

アクティビティはインターフェースを実装する必要がありますOnItemSelectedListener

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

これを行ったら、setlistener行を次のように変更できます。

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