0

我有两个 Activiti(Smena 和 Sdacha) 我按下 Smena 上的按钮并打开 Sdacha。我需要将字符串从 textView Sdacha 发送到 Smena。当我按下 Sdacha 上的按钮时,出现错误

public class Sdacha extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sdacha);    

        Button but10=(Button) findViewById(R.id.button10);
        but10.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        Intent intent1=new Intent(Sdacha.this,Smena.class);

                             startActivity(intent1);
                    }
            });

    }
}

>

 12-25 18:05:40.130: WARN/dalvikvm(378): threadid=1: thread exiting
> with uncaught exception (group=0x40014760) 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378): FATAL EXCEPTION: main 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378): java.lang.RuntimeException: Unable to start
> activity ComponentInfo{com.example.master/com.example.master.Smena}:
> java.lang.NullPointerException 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.access$500(ActivityThread.java:122) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.os.Handler.dispatchMessage(Handler.java:99) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.os.Looper.loop(Looper.java:132) 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.main(ActivityThread.java:4123) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> java.lang.reflect.Method.invokeNative(Native Method) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> java.lang.reflect.Method.invoke(Method.java:491) 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378):     at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> dalvik.system.NativeStart.main(Native Method) 12-25 18:05:40.151:
> ERROR/AndroidRuntime(378): Caused by: java.lang.NullPointerException
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> com.example.hotdog_master.Smena.onCreate(Smena.java:42) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.Activity.performCreate(Activity.java:4397) 12-25
> 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
> 12-25 18:05:40.151: ERROR/AndroidRuntime(378):     ... 11 more

Smena Activiti 这是收银员 Smena 活动代码的活动:

public class Smena extends Activity 
{
    public Nomenklatura Asortiment=new Nomenklatura(100);
    public static final String DEBUG_TAG = "myLogs";
    ArrayList<String> data =new ArrayList<String> ();
    ArrayList<String> data2 =new ArrayList<String> (4);
    GridView gvMain;
    GridView gvMain2;
    ArrayAdapter<String> adapter;
    ArrayAdapter<String> adapter2;
    double Sum=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smena); 
        Asortiment=(Nomenklatura) getIntent().getExtras().getSerializable("Nomenklatura");

        data2.add("СУММА");
        data2.add("0");
        data2.add("СДАЧА");
        data2.add("0");

        adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, data);
        gvMain = (GridView) findViewById(R.id.gridView1);
        gvMain.setAdapter(adapter);

        adapter2 = new ArrayAdapter<String>(this, R.layout.item2, R.id.tvText, data2);
        gvMain2 = (GridView) findViewById(R.id.gridView2);
        gvMain2.setAdapter(adapter2);

        Button but1=(Button) findViewById(R.id.button1);
        but1.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        ButtonOn (1);
                    }
            });
        Button but21=(Button) findViewById(R.id.button21);
        but21.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        ButtonDel();
                    }
            });

        Button but22=(Button) findViewById(R.id.button22);
        but22.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        data.clear();
                        adapter.clear();

                    }
            });

        Button but23=(Button) findViewById(R.id.button23);
        but23.setOnClickListener(
                new OnClickListener() 
                {
                    @Override
                    public void onClick(View arg0) 
                    {
                        Intent intent1=new Intent(Smena.this,Login.class);
                        // intent1.putExtra("Sdacha", Asortiment);
                         startActivity(intent1);

                    }
            });
    }
    private void ButtonDel()
    {
        int cnt=    data.size();
        data.remove(cnt-1);
        data.remove(cnt-2);
        adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, data);
        gvMain.setAdapter(adapter);
    }

    private void ButtonOn (int Art)
    {
        String name=    Asortiment.name.get(1);
        String price=   Asortiment.price.get(1).toString();
        data.add(name);
        data.add(price);
        adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, data);
        gvMain.setAdapter(adapter); 
    }
    private void CheckSum()
    {
        Sum=0;
        for(int k=1;k<data.size();k=k+2)
        {
            Sum=Sum+Double.parseDouble(data.get(k));
        }
    }

}
4

2 回答 2

0

请尝试这样

  startActivity(new Intent(this,Smena.class));
于 2012-12-25T12:21:43.477 回答
0

如果定义和声明正确,请检查以下内容:

1) 如果根据相应的 XML 的所有 id 都是正确的。

2) 活动在 Manifest 文件中定义。

3) 检查 Smena Activity 中的第 42 行是否有错误。

试试看,让我知道。如果可能,包括这些文件的代码以供进一步探索。

于 2012-12-25T12:53:15.507 回答