0

从哪里开始.. 我是 android 新手,正在尝试为 android 创建一个显示时间表(5 天 8 小时)的小应用程序,这些应用程序将显示为 40 个正方形的表格。用户可以通过选择日期(从微调器)、时间(微调器)、添加注释(最多 15 个字符)并选择背景颜色来添加事件。所有这些都必须添加到字符串数组并发送到主要活动。从那里通过从该字符串数组中获取日期和时隙值,相应的正方形将使用来自同一字符串数组的文本和背景颜色进行更新。我已经尝试解决这个问题大约四天了,但我无法理解这个问题......这是一些代码:主要活动:

package com.tt;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button settings = (Button)findViewById(R.id.settings);

        Intent a = getIntent();
        String[] add = a.getStringArrayExtra("strings");

        setContentView(R.layout.activity_main);
        TextView detail = (TextView)findViewById(R.id.m1);
        //detail.setText(notes);

        settings.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(v.getContext(),Settings.class);
                startActivityForResult(i,0);
            }
        });
    }

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

添加事件:

package com.tt;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

public class Add extends Activity implements OnClickListener, OnCheckedChangeListener {
    String[] DOW;
    String[] time;
    String  clr;
    EditText nts;
    boolean click = true;
    Button save;
    RadioGroup days;
    RadioGroup times;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        initialize();

        DOW = getResources().getStringArray(R.array.DOW);
        Spinner s1 =(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String>adapter = new      ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,DOW);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?>arg0,View arg1, int arg2, long arg3) {
                int index = arg0.getSelectedItemPosition();
                Toast.makeText(getBaseContext(), "Chosen Day of the Week is     "+ DOW[index], Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?>arg0){
                // nothing
            }
        });
        time = getResources().getStringArray(R.array.time);
        Spinner s2 = (Spinner)findViewById(R.id.spinner2);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,time);
        s2.setAdapter(adapter2);
        s2.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?>arg0,View arg1, int arg2, long arg3) {
                int index = arg0.getSelectedItemPosition();
                Toast.makeText(getBaseContext(), "Time is set to "+ time[index], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // nothing
            }
        });
    }
    private void initialize() {
        save = (Button)findViewById(R.id.save);
        save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String notes = nts.getText().toString();
                String day = DOW.toString();
                String tim = time.toString();
                //String color = clr.

                Bundle b = new Bundle();
                /*bag.putString("key1", day);
                //bag.putString("key2",tim);
                //bag.putString("key3", notes);
                //bag.putString("key4", color);*/

                /*String[] added = new String[4];
                added[0]=day;
                added[1]=tim;
                added[2]=color;
                added[3]=notes;*/

                //b.putStringArray("key",added);
                String[] added = new String[] {day, tim, notes};
                //b.putStringArray("key", new String[]{day, tim, notes});
                Intent a = new Intent(Add.this, MainActivity.class);
                a.putExtra("strings",added);
                startActivity(a);
            }
        });
        nts = (EditText) findViewById(R.id.notes);
        //days.setOnCheckedChangeListener((OnCheckedChangeListener) this);
        //times.setOnCheckedChangeListener((OnCheckedChangeListener) this);
    }

    @Override
    public void onClick(View arg0) {
        // nothing
    }

    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        switch(arg1) {
        case R.id.blue:
            clr = "blue";
            break;
        case R.id.green:
            clr = "green";
            break;
        case R.id.yellow:
            clr = "yellow";
            break;
        case R.id.red:
            clr = "red";
            break;
        }
    }
}

更新

对...我已经将字符串数组缩减为一个字符串...这是假设发送数据的代码:

    private void initialize() 
{
    save = (Button)findViewById(R.id.save);
    nts = (EditText) findViewById(R.id.notes);
}   
    public void onClick(View arg0) 
        {
            String notes = nts.getText().toString();
            //String day = DOW.toString();
            //String tim = time.toString();

            Bundle b = new Bundle();
            b.putString("AddedNote", notes);
            Intent a = new Intent(Add.this, MainActivity.class);
            a.putExtras(b);
            startActivity(a);
            /*String[] added = new String[] {notes};
            a.putExtra("strings",added);
            */
        };

而“接收者”:

    public class MainActivity extends Activity
{
String gotNotes;
TextView notes;
   @Override
    public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
    Button settings = (Button)findViewById(R.id.settings);
    Bundle gotPackage = getIntent().getExtras();
    gotNotes = gotPackage.getString("AddedNote");
    if (gotNotes != "")
    {
    notes.setText(gotNotes);
    }
    else{}


    settings.setOnClickListener(new OnClickListener()
    {



          public void onClick(View v)
         {
              Intent i = new Intent(v.getContext(),Settings.class);
              startActivityForResult(i,0);
         }

    });

   }

    private void initialize() 
   {
    // TODO Auto-generated method stub
    notes = (TextView)findViewById(R.id.m1);
}

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

    }

  }

我得到的结果是由行的 java.lang.nullpointerexception 引起的:

   gotNotes = gotPackage.getString("AddedNote");

“接收者”活动的..

4

1 回答 1

0

这取决于你的活动 android:launchMode,我怀疑你有 singleTask 或类似的。在这种情况下,您应该覆盖 onNewIntent,您应该使用字符串数组接收意图。

另一种解决方案是将数据存储在共享首选项中,如果需要,您可以将其序列化为 JSON 对象。

protected void onNewIntent(Intent intent) {
   String[] add = a.getStringArrayExtra("strings");
}
于 2012-12-24T15:43:14.900 回答