当我想打开新意图时,我的应用程序不断崩溃

时间:2017-06-11 09:17:37

标签: java android android-intent

每当我想打开新意图时,我的应用都会崩溃。我也没有收到任何错误,只是停止了。

我的主要活动:

package nl.mirjamvanmourik.hueemulator;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements 
LightBulbTaskListener, AdapterView.OnItemClickListener {

private final Bridge bridge = Bridge.getInstance();
private final ArrayList<LightBulb> list = new ArrayList<>();

private ListView listView;
private LightBulbAdapter adapter;

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

    listView = (ListView) findViewById(R.id.lightBulbsList);
    adapter = new LightBulbAdapter(this, list);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);

    fetchLights();
    }

    public void fetchLights(){
    try {
        list.clear();
        URL[] url = new URL[]{
                new URL (getString(R.string.BASE_URL) + 
getString(R.string.USERNAME) + "/lights/")
        };

        LightBulbTask.getLights w = new LightBulbTask.getLights(this);
        w.execute(url);
    } catch (IOException e) {
        System.out.println("Error:> " + e);
    }
}

public void newLightBulb(LightBulb light){
    list.add(light);
    adapter.notifyDataSetChanged();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    LightBulb light = (LightBulb) list.get(position);
    Intent intent = new Intent(getApplicationContext(), 
LightBulbDetails.class);
    intent.putExtra("LIGHTBULB", light);
    startActivity(intent);
}
}

My LightBulbDetails类:

package nl.mirjamvanmourik.hueemulator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.SeekBar;
import android.widget.TextView;

/**
 * Created by mirja on 10-6-2017.
 */

public class LightBulbDetails extends AppCompatActivity {

    private Bridge bridge = Bridge.getInstance();
    private LightBulb light;
    private TextView label;
    private SeekBar hueSeekBar;
    private SeekBar satSeekBar;
    private SeekBar briSeekBar;

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

        light = (LightBulb) getIntent().
                getSerializableExtra("LIGHTBULB");
        label = (TextView) findViewById(R.id.labelTextView);
        label.setText(light.getDescr());

        hueSeekBar = (SeekBar) findViewById(R.id.hueSeekBar);
        hueSeekBar.setProgress(light.getHue());
        hueSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                light.setHue(hueSeekBar.getProgress());
            }
        });

        satSeekBar = (SeekBar) findViewById(R.id.satSeekBar);
        satSeekBar.setProgress(light.getSat());
        satSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                light.setSat(satSeekBar.getProgress());
            }
        });

        briSeekBar = (SeekBar) findViewById(R.id.briSeekBar);
        briSeekBar.setProgress(light.getBri());
        briSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                light.setBri(briSeekBar.getProgress());
            }
        });
    }
}

我的light_details 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"
    android:paddingBottom="16dp"
    android:paddingLeft="64dp"
    android:paddingRight="64dp"
    android:paddingTop="16dp"
    tools:context="nl.mirjamvanmourik.hueemulator.LightBulbDetails">

    <TextView
        android:id="@+id/labelTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/hueTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hue: " />

    <SeekBar
        android:id="@+id/hueSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="65535" />

    <TextView
        android:id="@+id/satTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Saturation: " />

    <SeekBar
        android:id="@+id/satSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="254" />

    <TextView
        android:id="@+id/briTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Brightness: " />

    <SeekBar
        android:id="@+id/briSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="254" />

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pas aan" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Annuleer" />

    </LinearLayout>

</LinearLayout>

我已将活动添加到我的清单中。

提前致谢!!

2 个答案:

答案 0 :(得分:1)

  

我认为您还没有使用Serealizable,请确保您拥有   使用Serealizable实现了LightBulb类,如下所示

class LightBulb implements Serealizable

答案 1 :(得分:0)

您的灯泡类必须实现Serializable

例如

class LightBulb implements Serealizable{
     // Your Implementation
}

然后你可以将LightBulb对象作为额外的。

LightBulb light = (LightBulb) list.get(position);
Intent intent = new Intent(getApplicationContext(), LightBulbDetails.class);
intent.putExtra("LIGHTBULB", light);
startActivity(intent);

你可以从LightBulbDetails那样检索

light = (LightBulb) getIntent().getSerializableExtra("LIGHTBULB");